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

前端 未结 3 1823
遇见更好的自我
遇见更好的自我 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:38

    This is a little snippet I use to determine the scrolltop of a page, I can't remember where I got it, or whether I wrote it myself so can't credit it.

    var st=0;
    if(typeof pageYOffset!= 'undefined'){
        //most browsers
        st = pageYOffset;
    } else {
        var B = document.body; //IE 'quirks'
        var D = document.documentElement; //IE with doctype
        D = (D.clientHeight)? D: B;
        st = D.scrollTop;
    }
    

    if st==0 then the user is at the top of the page!

提交回复
热议问题