[英]prevent animation double click issue
Hi I have problem with my slider please visit this site and check http://paruyr.bl.ee/ after click on my arrows it becomes work in an asynchronous way, ones it changes very fast and then slow and it repeats. 嗨,我的滑盖出现问题,请访问此网站并检查http://paruyr.bl.ee/。在单击我的箭头后,它以异步方式开始工作,它变化得非常快,然后又变慢,然后重复。 I think it is from start slider and stop slider.
我认为这是从开始滑块和停止滑块开始的。
var sliderPrev = 0,
sliderNext = 1;
$("#slider > img").fadeIn(1000);
startSlider();
function startSlider(){
count = $("#slider > img").size();
loop = setInterval(function(){
if (sliderNext>(count-1)) {
sliderNext = 0;
sliderPrev = 0;
};
$("#slider").animate({left:+(-sliderNext)*100+'%'},900);
sliderPrev = sliderNext;
sliderNext=sliderNext+1;
},6000)
}
function prev () {
var newSlide=sliderPrev-1;
showSlide(newSlide);
}
function next () {
var newSlide=sliderPrev+1;
showSlide(sliderNext);
}
function stopLoop () {
window.clearInterval(loop);
}
function showSlide(id) {
stopLoop();
if (id>(count-1)) {
id = 0;
} else if(id<0){
id=count-1;
}
$("#slider").animate({left:+(-id)*100+'%'},900);
sliderPrev = id;
sliderNext=id+1;
startSlider();
};
$("#slider, .arrows").hover(function() {
stopLoop()
}, function() {
startSlider()
});
function onlyNext () {
var newSlide=sliderPrev+1;
onlyShowSlide(newSlide);
}
function onlyShowSlide(id) {
if (id>(count-1)) {
id = 0;
} else if(id<0){
id=count-1;
}
$("#slider").animate({left:+(-id)*100+'%'},900);
sliderPrev = id;
sliderNext=id+1;
};
It is occurring because your animations are being queued. 这是因为您的动画正在排队。
Try adding: 尝试添加:
.stop( true, true )
Before each of your animation methods. 在每种动画方法之前。 ie
即
$("#slider").stop( true, true ).animate({left:+(-id)*100+'%'},900);
What I would do is add a class to your slider when the animation starts and remove the class when it finishes: 我要做的是在动画开始时向滑块添加一个类,并在结束时删除该类:
$("#slider").animate({left:+(-id)*100+'%'}, {
duration: 900,
start: function() {
$('#slider').addClass('blocked');
},
complete: function() {
$('#slider').removeClass('blocked');
}
});
Now check on each click event if the slider is blocked or not: 现在检查每个单击事件是否滑块被阻止:
function next () {
if (!$('#slider').hasClass('blocked')) {
var newSlide=sliderPrev+1;
showSlide(sliderNext);
}
}
This is a very simple solution, I'm sure there is a better one. 这是一个非常简单的解决方案,我敢肯定会有更好的解决方案。
EDIT: As marcjae pointed out, you could stop the animations from queuing. 编辑:正如marcjae所指出的,您可以停止动画的排队。 This means when you double click, the slideshow still will move 2 slides.
这意味着当您双击时,幻灯片放映仍将移动2张幻灯片。 With my approach the second click will be ignored completely.
用我的方法,第二次单击将被完全忽略。
You can use a variable flag to control if the animation is still being done, or simply use .stop() to avoid stacking the animation. 您可以使用变量标志来控制动画是否仍在进行中,或者只是使用.stop()来避免堆叠动画。
$("#pull").click(function(){
$("#togle-menu").stop().slideToggle("slow");
});
I think the best option would be to check if the animation is in progress and prevent the action if it is, something like this: 我认为最好的选择是检查动画是否在进行中,并阻止动画的出现,例如:
function prev () {
if(!$('#slider').is(":animated"))
{
var newSlide=sliderPrev-1;
showSlide(newSlide);
}
}
function next () {
if(!$('#slider').is(":animated"))
{
var newSlide=sliderPrev+1;
showSlide(sliderNext);
}
}
To illustrate the difference between this and just sticking a stop()
in, check this JSFiddle . 为了说明这一点与仅插入
stop()
之间的区别,请检查此JSFiddle 。 You will notice some choppy movements if you click multiple times in the stop()
version. 如果在
stop()
版本中单击多次,您会发现一些不稳定的动作。
The answers about stop
are good, but you have a bigger issue that is causing the described behavior. 有关
stop
的答案很好,但是您遇到的更大问题导致了所描述的行为。 The issue is here: 问题在这里:
$("#slider, .arrows").hover(function() {
stopLoop()
}, function() {
startSlider()
});
You have bound this to the .arrows
as well as the #slider
and the arrows are contained within the slider. 你已经束缚,这给
.arrows
还有#slider
和箭头都包含在滑盖内部。 So, when you mouse out of an arrow and then out of the entire slider, you are calling start
twice in a row without calling stop
between. 因此,当您从箭头上移出鼠标,然后从整个滑块上移出时,您连续两次调用了
start
,而没有在其间调用stop
。 You can see this if you hover onto the arrow and then off of the slider multiple times in a row. 如果将鼠标悬停在箭头上,然后连续多次离开滑块,则可以看到此内容。 The slides will change many times after 6 seconds.
幻灯片将在6秒后更改很多次。
Similarly, consider the case of a single click: 同样,考虑一下单击的情况:
Enter the `#slider` [stopLoop]
Enter the `.arrows` [stopLoop]
Click the arrow [stopLoop]
[startSlider]
Leave the `.arrows` [startSlider]
Leave the `#slider` [startSlider]
As you can see from this sequence of events, startSlider
is called 3 times in a row without calling stopLoop
inbetween. 正如你可以从事件的顺序看,
startSlider
被称为连续3次,而无需调用stopLoop
插图中。 The result is 3 intervals created, 2 of which will not be stopped the next time stopLoop
is called. 结果是创建了3个时间间隔,其中2个将在下次调用
stopLoop
停止。
You should just have this hover
on the #slider
and more importantly, add a call to stopLoop
as the first step in startSlider
. 你应该只有这个
hover
在#slider
,更重要的是,呼吁加stopLoop
作为第一步startSlider
。 That will ensure that the interval is always cleared before creating a new one. 这样可以确保在创建新间隔之前始终清除间隔。
$("#slider").hover(function() {
stopLoop()
}, function() {
startSlider()
});
function startSlider(){
stopLoop();
/* start the slider */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.