How to get scrollbar position with Javascript?

前端 未结 9 2267
自闭症患者
自闭症患者 2020-11-22 11:23

I\'m trying to detect the position of the browser\'s scrollbar with JavaScript to decide where in the page the current view is. My guess is that I have to detect where the t

9条回答
  •  太阳男子
    2020-11-22 11:55

    document.getScroll = function() {
        if (window.pageYOffset != undefined) {
            return [pageXOffset, pageYOffset];
        } else {
            var sx, sy, d = document,
                r = d.documentElement,
                b = d.body;
            sx = r.scrollLeft || b.scrollLeft || 0;
            sy = r.scrollTop || b.scrollTop || 0;
            return [sx, sy];
        }
    }
    

    returns an array with two integers- [scrollLeft, scrollTop]

提交回复
热议问题