javascript - How do i detect when a user is at the top of the webpage

前端 未结 3 1791
遇见更好的自我
遇见更好的自我 2021-01-23 08:02

I\'m pretty much completely new to javascript, and I know there\'s already a similar question to this on here, but i would like the script as in.

if (user is at top of

3条回答
  •  时光取名叫无心
    2021-01-23 08:28

    I'm using a function to make it cross-browser compatible that can be found here: Cross-browser method for detecting the scrollTop of the browser window

    function getScrollTop(){
        if(typeof pageYOffset!= 'undefined'){
            //most browsers
            return pageYOffset;
        }
        else{
            var B= document.body; //IE 'quirks'
            var D= document.documentElement; //IE with doctype
            D= (D.clientHeight)? D: B;
            return D.scrollTop;
        }
    }
    
    if(!getScrollTop()){
       // user is at the top
    }
    

    Here is a little demo: http://jsfiddle.net/uDS4n/1/

提交回复
热议问题