[英]I want to prevent a second click from firing until an animation is complete using bind and unbind but it isnt working
I want to prevent a second click from firing until an animation is complete. 我想防止在动画完成之前触发第二次单击。
$('#moveDown').click(function(){
var currentPos = parseInt($('#blog-slider').css('top'));
if (currentPos < 0) {
$('#moveDown').unbind('click');
$('#blog-slider').animate({'top': currentPos + 140},500, function() {
$('#moveDown').bind('click');
});
}
});
Unbind is working fine, but it is not rebinding when the animation is complete, so the animation will only run once. 取消绑定可以正常工作,但是在动画完成时它不会重新绑定,因此动画将只运行一次。
I did look here but this only confirms that "bind" and "unbind" is the way to get the functionality I need and I still don't understand why it isn't working. 我确实在这里看过,但这仅确认“绑定”和“解除绑定”是获得所需功能的方式,但我仍然不明白为什么它不起作用。
$('#moveDown').bind('click')
just triggers a click, unless you bind a function that does something. $('#moveDown').bind('click')
只会触发一次单击,除非您绑定执行某项功能的函数。
One technique is to set and unset a .data
variable on the element, and check if it's set or not: 一种技术是在元素上设置和取消设置.data
变量,然后检查是否已设置:
$('#moveDown').click(function(){
if (!$('#moveDown').data('clicked')) {
$('#moveDown').data('clicked',true);
var currentPos = parseInt($('#blog-slider').css('top'),10); // always use a radix
if (currentPos < 0) {
$('#blog-slider').animate({'top': currentPos + 140},500, function() {
$('#moveDown').data('clicked',false);
});
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.