简体   繁体   English

JQUERY scrollTo执行后不会让我滚动

[英]JQUERY scrollTo won't let me scroll after executed

Im using the following JQUERY to hide an element upon scroll and then scroll back to the top of the page to display the new element: 我使用以下JQUERY在滚动时隐藏了一个元素,然后滚动回到页面顶部以显示新元素:

$(window).bind('scroll', function() {
 if ($(window).scrollTop() > 200) {
 }
 else {
    $("#hero-area").slideUp(1300);
    $(".timeline").show(2000);
    $("body").animate({ scrollTop: 0 }, "slow");
 return false;
 }
});

Problem is, it scrolls back to the top of the page just fine but after execution it refuses to let me continue scrolling the page. 问题是,它可以正常滚动回到页面顶部,但执行后拒绝让我继续滚动页面。 I have tried returning false, but no luck. 我尝试返回假,但没有运气。 Maybe because im using an IF statement? 也许是因为我使用了IF语句?

--Update-- One approach to your question, load once is to set a global variable and check it before you run it like so... -更新-解决问题的一种方法,加载一次就是设置一个全局变量,然后像这样运行它之前检查它...

    var canRun = true; //declare a global variable
    $(function () { //on page load
        $(window).bind('scroll', function () {
            if ($(window).scrollTop() > 200) {
                if (canRun) {
                    //Do your animation
                    //set the global to false
                    canRun = !canRun;
                }
            }
        });
    });

If $(window).scrollTop() = 0 it would go to the else block and then try to scroll up again.Just looooooooping. 如果$(window).scrollTop() = 0 ,它将转到else块,然后再次尝试向上滚动。

If you want to play with the "#hero-area" element after passing 200 then you want to get rid of your else block, take that code put it inside if and not worry about the rest. 如果要在传递200之后使用"#hero-area"元素,那么您想摆脱else块,可以将该代码放入其中if而不必担心其余的代码。

Hope that helps. 希望能有所帮助。 If that didn't answer your question let me know. 如果那不能回答您的问题,请告诉我。

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

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