Jquery 仅在动画完成后激活按钮上的事件
我创建了一个按钮,当它被单击时,会移动一些图像,但当我快速单击按钮时,jQuery会在图像的移动上创建一个错误。因此,我希望禁用按钮,直到.animate()函数未完成。我试图写这段代码,但不正确Jquery 仅在动画完成后激活按钮上的事件,jquery,animation,Jquery,Animation,我创建了一个按钮,当它被单击时,会移动一些图像,但当我快速单击按钮时,jQuery会在图像的移动上创建一个错误。因此,我希望禁用按钮,直到.animate()函数未完成。我试图写这段代码,但不正确 var next = $('.next'); function nextAction(e){ e.preventDefault(); next.off('click'); if(pellicle.css('left').replace(/[^-\d\.]/g, '') <
var next = $('.next');
function nextAction(e){
e.preventDefault();
next.off('click');
if(pellicle.css('left').replace(/[^-\d\.]/g, '') <= -(stepTotalWidth - screenWidth)){
pellicle.animate({
left:'0'
},1000, function(){next.on('click', nextAction);});
}
else {
pellicle.animate({
left:'-=' + screenWidth
},1000);
}
}
next.on('click', nextAction);
var next=$('.next');
函数nextAction(e){
e、 预防默认值();
next.off('click');
如果(pellicle.css('left').replace(/[^-\d\.]/g,”)您可以附加和分离任意数量的事件,我没有得到什么问题,除了当您在动画运行时“关闭”事件时,您在单击期间有问题
为此,您可以附加另一个事件,该事件将阻止默认设置,第二个事件将运行默认设置
function nextAction(e){
next.off('click.gallery'); //detach by namespace
next.off('click', nextAction); //detach by link on function
//Your code and again attach animation click
}
next.on('click', function(e){
e.preventDefault();
});
next.on('click.gallery', nextAction);
/* .gallery is not necessary if you will detach events
by the link on the function that attached to that */
第二件事是,pellicle.css('left')
总是返回***px,在这种情况下,比regexp的parseInt(pellicle.css('left'),10)| | 0要快一点。在这种情况下,它总是会给你一个数字。通过触发器的完美解决方案,这里有完整的代码
//next button
function nextAction(e){
next.off('click', nextAction); //detach by link on function
e.preventDefault();
if(parseInt(pellicle.css('left')) <= -(stepTotalWidth - screenWidth)){
pellicle.animate({
left:'0'
},1000, function(e){next.on('click', nextAction);});
}
else {
pellicle.animate({
left:'-=' + screenWidth
},1000, function(e){next.on('click', nextAction);});
}
}
next.on('click', nextAction);
//下一步按钮
函数nextAction(e){
next.off('click',nextAction);//通过函数上的链接分离
e、 预防默认值();
如果(parseInt(pellicle.css('left'))我的代码不起作用,从某种意义上说,它没有禁用按钮问题是如何在动画开始时分离事件,以及在动画结束后如何附加事件。我想分离iPhone幻灯片上的事件按钮。