Cross-browser method for detecting the scrollTop of the browser window

前端 未结 11 1728
孤街浪徒
孤街浪徒 2020-11-27 12:38

What is the best cross-browser way to detect the scrollTop of the browser window? I prefer not to use any pre-built code libraries because this is a very simple script, and

相关标签:
11条回答
  • 2020-11-27 13:14

    YUI 2.8.1 code does this:

    function getDocumentScrollTop(doc) 
    {
       doc = doc || document;
    
       //doc.body.scrollTop is IE quirkmode only
       return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
    }
    

    I think jQuery 1.4.2 code (a bit translated for humans) and supposing I understood it properly does this:

    function getDocumentScrollTop(doc) 
    {
       doc = doc || document;
       win = doc.defaultView || doc.parentWindow; //parentWindow is for IE < 9
    
       result = 0;
       if("pageYOffset" in win) //I'don't know why they use this, probably they tested it to be faster than doing: if(typeof win.pageYOffset !== 'undefined')
          result = win.pageYOffset;
       else
          result = (jQuery.support.boxModel && document.documentElement.scrollTop) || 
                   document.body.scrollTop;
    
       return result;
    }
    

    Unfortunatley extracting the value of jQuery.support.boxModel is almost impossible because you would have to add a temporary child element into document and do the same tests jQuery does.

    0 讨论(0)
  • 2020-11-27 13:17
    function getScrollTop(){
        if(typeof pageYOffset!= 'undefined'){
            //most browsers except IE before #9
            return pageYOffset;
        }
        else{
            var B= document.body; //IE 'quirks'
            var D= document.documentElement; //IE with doctype
            D= (D.clientHeight)? D: B;
            return D.scrollTop;
        }
    }
    
    alert(getScrollTop())
    
    0 讨论(0)
  • 2020-11-27 13:17
    function getSize(method) {
      return document.documentElement[method] || document.body[method];
    }
    getSize("scrollTop");
    
    0 讨论(0)
  • 2020-11-27 13:18

    I know its been quite a while since this thread was updated, but this is a function I created that allows developers to find the root element that actually hosts has a working "scrollTop" property. Tested on Chrome 42 and Firefox 37 for Mac OS X (10.9.5):

    function getScrollRoot(){
        var html = document.documentElement, body = document.body,
            cacheTop = ((typeof window.pageYOffset !== "undefined") ? window.pageYOffset : null) || body.scrollTop || html.scrollTop, // cache the window's current scroll position
            root;
    
        html.scrollTop = body.scrollTop = cacheTop + (cacheTop > 0) ? -1 : 1;
        // find root by checking which scrollTop has a value larger than the cache.
        root = (html.scrollTop !== cacheTop) ? html : body;
    
        root.scrollTop = cacheTop; // restore the window's scroll position to cached value
    
        return root; // return the scrolling root element
    }
    
    // USAGE: when page is ready: create a global variable that calls this function.
    
    var scrollRoot = getScrollRoot();
    
    scrollRoot.scrollTop = 10; // set the scroll position to 10 pixels from the top
    scrollRoot.scrollTop = 0; // set the scroll position to the top of the window
    

    I hope you find this useful! Cheers.

    0 讨论(0)
  • 2020-11-27 13:22

    No need extra library, use this snippet:

    const scrollTop = 
        (window.pageYOffset !== undefined) ? 
         window.pageYOffset :
        (document.documentElement || document.body.parentNode || document.body).scrollTop;
    
    0 讨论(0)
提交回复
热议问题