Javascript 在没有jQuery的情况下查找滚动条的垂直位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11193453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the vertical position of scrollbar without jQuery
提问by ama2
I'm trying to find the vertical position of the scrollbar a function similar to jQuery's scrollTop() but with no jQuery. Are there any alternatives?
我试图找到一个类似于 jQuery 的 scrollTop() 但没有 jQuery 的函数的滚动条的垂直位置。有没有其他选择?
回答by Engineer
Cross-browser solution:
跨浏览器解决方案:
var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
var scrollLeft = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft;
var scrollTop = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
回答by Hyman Stone
Yes, the scroll position exists within the DOM at:
是的,滚动位置存在于 DOM 中:
window.scrollY; //for vertical scroll.
How I found this:
我是如何找到这个的:
- In Chrome, right click and select Inspect Element.
- Find and click the 'Show Console' button (lower-left)
- In the console type window.scroll to see options.
- 在 Chrome 中,右键单击并选择检查元素。
- 找到并单击“显示控制台”按钮(左下角)
- 在控制台中输入 window.scroll 来查看选项。
-This is an exceptional workflow to solve a multitude of JavaScript questions.
- 这是解决大量 JavaScript 问题的出色工作流程。
I see window.scrollTo(0) as an option to scroll to top.
我看到 window.scrollTo(0) 作为滚动到顶部的选项。
回答by Trey
scrollX and scrollY.... scrollY is the equivalent of jquery scrollTop()
scrollX 和 scrollY .... scrollY 相当于 jquery scrollTop()