【发布时间】:2016-07-21 16:07:13
【问题描述】:
简单的问题:所以我有一个mouseenter 函数
$('.filmstrip').bind('mouseenter',function(){
var isStopped = false;
var $that = $(this),
w = $that.width(),
fr = $that.attr('data-framerate');
$that.css('background-position',$that.attr('data-placeholderXStart') + ' center');
$that.css('background-image','url('+$that.attr('data-gifurl')+')');
for ( var i = 1, n = $that.attr('data-ticks'); i <= n && !isStopped; ++i )
{
(function(j){
setTimeout(function(){
if (!isStopped) {
$that.css('background-position','-'+(w*j)+'px center');
}
}, j*fr);
})(i);
}
$that.bind('mouseleave',function(){
isStopped = true;
$that.css('background-image','url('+$that.attr('data-placeholder')+')').css('background-position',$that.attr('data-placeholderXStart') + ' center');
});
});
并且我希望它仅在它尚未处于执行过程中时才执行。原因是因为我不希望有人将鼠标重新悬停在该事物上并使其在其仍处于动画状态时启动。
【问题讨论】:
-
将
isStopped放在事件处理程序之外。 -
@adeneo 所以让它成为一个全局变量?
-
这取决于事件处理程序之外的范围。我个人会使用 jQuery 的
data()。然而,该变量确实需要在事件处理程序之外持续存在,因此如果再次触发处理程序,它就知道动画正在进行中
标签: javascript jquery dom events jquery-events