当前位置: 动力学知识库 > 问答 > 编程问答 >

vertical document scroll position in JavaScript that is cross-browser compatible up to IE 8

问题描述:

This question already has an answer here:

  • How to get scrollbar position with Javascript?

    4 answers

  • How to scroll to top of page with JavaScript/jQuery?

    22 answers

网友答案:

Use the source:

var scrollPosition = window.pageYOffset;

Compatibility notes:

  • document.body.scrollTop doesn’t work (is always 0) in Firefox; it’s document.documentElement.scrollTop if you want to use a scrollTop at all. Conversely, document.documentElement.scrollTop is always 0 in Chrome (well, Blink and WebKit).

  • window.scrollY doesn’t exist in IE 8 and earlier.

So, for old IE compatibility, use:

var scrollPosition =
    'pageYOffset' in window ?
        window.pageYOffset :
        document.body.scrollTop;
分享给朋友:
您可能感兴趣的文章:
随机阅读: