简体   繁体   English

jQuery在scrollTop函数上的延迟

[英]jQuery delay on scrollTop function

I'm trying to hide with an .animate a block when the user scroll to the top on the html, the thing is that when I got to the top it takes like 5sec to make the animation, I want the animation to trigger immediately, someone know how please? 当用户滚动到html顶部时,我试图用.animate块隐藏。问题是,当我到达顶部时,制作动画大约需要5秒,我希望动画立即触发,有人知道如何吗?

// On scroll to top hide block 'Welcome'
$(document).scroll(function () {
    if ($(window).scrollTop() === 0) {
        $("div#welcome_slide").animate({ "top": "0" }, 500);
    }
});

Too late but here's my answer anyway. 为时已晚,但是无论如何这是我的答案。

.scroll() is triggered MANY times whilst the user is scrolling. 用户滚动时,将多次触发.scroll() Therefore your animation is getting triggered LOTS of times. 因此,您的动画被触发了很多次。 These animations are queued up - hence causing the long delay when hiding (its waiting till the end of the queue). 这些动画排队等候-这样会导致隐藏时的长时间延迟(等待直到队列结束)。

You can force the queue to end using .stop() to abort the animation before each call OR you can set a flag to test if the slide is revealed and only animate it accordingly: 您可以使用.stop()强制队列结束以在每次调用之前中止动画,或者可以设置一个标志来测试幻灯片是否显示并仅对其进行动画处理:

$(window).scroll(function () {

    // On scroll show block 'Welcome'
    if ($(this).scrollTop() >= 100 && !$(this).data('revealed')) {
        $("#slide").stop().animate({ "top": "100px" }, 1000);
        $(this).data('revealed',true);
        return false;
    }

    // On scroll to top hide block 'Welcome'
    if ($(this).scrollTop() === 0) {
        $("#slide").stop().animate({ "top": "480px" }, 500);            
        $(this).removeData("revealed");
        return false;
    }
});

http://jsfiddle.net/574arm1w/3/ http://jsfiddle.net/574arm1w/3/

I have updated your fiddle 我已经更新了你的小提琴

Please check if this is what you're trying to achieve. 请检查这是否是您要实现的目标。

UPDATED CODE 更新的代码

    var  animating = false;    
    $(window).scroll(function () {
        var divTop = Number($('#slide').css('top').replace('px',''));
        if ($(this).scrollTop() >= 100 && !animating && divTop > 100) {
            // On scroll show block 'Welcome'
            animating = true;
            $("#slide").animate({ "top": 100+"px" }, 1000,function(){
            animating = false;
            });
            return false;
        }
        else if ($(this).scrollTop() == 0 && !animating && divTop < 480) {
                // On scroll to top hide block 'Welcome'
            console.log('0');
            animating = true;
            $("#slide").animate({ "top": 480+"px" }, 500,function(){
            animating = false;
            });
           return false;
        }        
    });   

You can use .delay in jquery 您可以在jquery中使用.delay

$("div#welcome_slide").delay(1000).animate({ "top": "0" }, 500);

or setTimeout setTimeout

setTimeout((function() {
   $("div#welcome_slide").animate({top: 0} ,{duration:500});
}), 1000);

Try this code.I think this is your requirement. 试试这个代码。我认为这是您的要求。

$(document).scroll(function () {    
if ($(window).scrollTop() === 0) {    
    $("div#welcome_slide").slideUp(500);    
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM