【问题标题】:Using stop() to stop the jquery lag使用 stop() 停止 jquery 滞后
【发布时间】:2013-08-06 04:21:25
【问题描述】:

我知道我应该使用 $(selector).stop().animate();某处,但我不确定当你离开时如何停止延迟,并且非常快地打开它,它会继续动画。这是我的代码,如果你能告诉我该放什么,我会设置(记住我是 jquery 的初学者)。

$(document).ready(function(){

var order = $('#order').mouseenter(function(){
 $('#animateorder').animate({top: '+=5.5em'}, 'fast')
});

var order2 = $('#order').mouseleave(function(){

 $('#animateorder').animate({top: '-=5.5em'}, 'fast')
});

});
$(document).ready(function(){
 $('#contact').mouseenter(function(){
  $('#animatecontact').animate({top: '+=5.5em'}, 'fast')
 });
$('#contact').mouseleave(function(){
  $('#animatecontact').animate({top: '-=5.5em'}, 'fast')
 });
});

【问题讨论】:

  • 你在$(selector).stop().animate(); 中对你的代码有什么不明白的地方???
  • 除非您在谈论 FIX 金融协议,否则请不要使用“修复”标签。

标签: jquery animated-gif lag


【解决方案1】:

一种解决方案是使用计时器稍微延迟 mouseenter 操作,这样如果用户快速移入和移出鼠标,则不会发生任何事情。像这样的东西,虽然我可能没有涵盖所有的细节。但这应该让您对如何进行有所了解。

var timer = null;
$('#contact).mouseenter(function() {
    timer = setTimeout(function() {
        $('#animatecontact').animate({top: '+=5.5em'}, 'fast');
        timer = null;
    }, 250);  // Adjust to fit your needs best
});

$('#contact').mouseleave(function() {
    if(timer !== null) {
        clearTimeout(timer);
        timer = null;
    }

    $('#animatecontact').animate({top: '-=5.5em'}, 'fast');
});

【讨论】:

  • 现在它根本没有动画?
  • 就像我说的,我不打算涵盖所有细节。尝试一些调试——Chrome 调试器非常棒。您可以在这些函数的关键点使用 console.log() 来找出问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多