使用stop()来停止jquery延迟

时间:2013-08-05 18:55:56

标签: jquery animated-gif lag

我知道我应该使用$(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')
 });
});

1 个答案:

答案 0 :(得分:0)

一种解决方案是使用计时器稍微延迟鼠标中心操作,以便在用户快速移入和移出鼠标时没有任何反应。这样的事情,虽然我可能没有涵盖所有的细节。但这应该会让你知道如何继续。

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');
});