我用javascript和css做了一个旋转滑块。Javascript函数使其旋转。当我第一次加载页面时,它工作得很好,但当我导航到其他页面并返回时,js函数会被调用,但滑块在鼠标悬停时不会停止旋转。我做了一个js函数,当鼠标悬停在div上时停止旋转,当它离开时这个函数被称为again.Here是我的js代码。
var timer;
window.onload = function()
{
myTimer();
};
var $i=1;
function myTimer(){
timer=setInterval(function(){
if($i<6){
closeTab();
openTab($i);
$i++;
}
else
{$i=1;}
},2000);
}
closeTab();
var selectedSlider ;
function closeTab()
{
var $this = $('#accordion > li');
//$width=$(window).width();
//$this.stop().animate({'width':'110px'},500);
$this.stop().animate({'width':'7%'},500);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
}
function openTab(id)
{
var $this = $('.bg'+id);
//$this.stop().animate({'width':'450px'},500);
$this.stop().animate({'width':'70%'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
}
openTab(1);
$(function() {
$('#accordion > li').hover(
function () {
console.log("in mouse hover");
var $this = $(this);
selectedSlider = $this.attr("class").replace("bg", "");
closeTab();
openTab(selectedSlider);
clearInterval(timer);
},
function () {
}
);
});
$('#accordion > li').mouseleave(function(){
console.log("in mouse leave");
myTimer();
});发布于 2014-01-21 14:30:26
因为@David说浏览器正在缓存javascript,所以你的悬停效果代码并不像你所说的那样工作,当js文件调用时,hover事件被激发,但这里的js文件已经被缓存了。
您可以尝试这样做,而不是使用悬停函数匿名地将其放入window.onload中。
https://stackoverflow.com/questions/21250591
复制相似问题