jquery setInterval or scroll

前端 未结 2 1038
感动是毒
感动是毒 2021-01-02 07:12

I am working on a project where i need to listen to the scroll event.. i wonder what is a better approach..

1st Approach

 function scr         


        
2条回答
  •  悲哀的现实
    2021-01-02 07:56

    Neither. I was just reading about JS/jQuery patterns. There is an example for the Window Scroll event: jQuery Window Scroll Pattern

    var scrollTimeout;  // global for any pending scrollTimeout
    
    $(window).scroll(function () {
        if (scrollTimeout) {
            // clear the timeout, if one is pending
            clearTimeout(scrollTimeout);
            scrollTimeout = null;
        }
        scrollTimeout = setTimeout(scrollHandler, 250);
    });
    
    scrollHandler = function () {
        // Check your page position
        if ($(window).scrollTop() > 200) {
            top.fadeIn();
        } else {
            top.fadeOut();
        }
        if (menuVisible) {
            quickHideMenu();
        }
    };
    

    Originally from here: Javascript Patterns

提交回复
热议问题