jQuery:在动画期间禁用点击
所以我正在进行一个小测验,我希望在动画运行时禁用#qWrap
内部的所有内容,从而防止垃圾邮件点击。 我尝试使用.is(':animated')
但没有效果。 有任何想法吗?
HTML:
-
-
-
Who designed Comic Sans?
-
Mathew Carter
-
David Carson
-
Vincent Connare
-
-
What does Helvetica mean?
-
From Austria
-
From Germany
-
From Switzerland
JS:
$('.qAnswers li a').bind('click', function(event) { $(this).parent().addClass('selected'); $(this).closest('.qAnswers').find("li:not(.selected, .qQuestion)").delay(200).addClass('notSelected'); var $anchor = $(this); //preventing click within #qWrap if ($('#qWrap').is(':animated')) { $('#qWrap').unbind('click'); } //firing the animation $('#qWrap').stop(true, true).delay(800).animate({ scrollLeft: $($anchor.attr('href')).position().left }, 2000, function() { nextCount(); }); stopTimer(); addData(); event.preventDefault(); });
您的JS代码应如下所示:
$('.qAnswers li a').bind('click', function(event) { event.preventDefault(); //preventing click within #qWrap if ($('#qWrap').is(':animated')) { return; } $(this).parent().addClass('selected'); $(this).closest('.qAnswers').find("li:not(.selected, .qQuestion)").delay(200).addClass('notSelected'); var $anchor = $(this); //firing the animation $('#qWrap').stop(true, true).delay(800).animate({ scrollLeft: $($anchor.attr('href')).position().left }, 2000, function() { nextCount(); }); stopTimer(); addData(); });
代码$('#qWrap').is(':animated')
不是在元素动画时触发的事件,它是正常检查。 我移动了你的event.preventDefault();
到顶部; 想你想做到这一点,无论如何。 您可能还需要移动其他一些东西。
$('[where you click]').click( function(){ if(!$('[what animates]').is(':animated')){ $('[what animates]').[your_animation]; } });