JavaScript 获取滚动的窗口 X/Y 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3464876/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:45:03  来源:igfitidea点击:

JavaScript get window X/Y position for scroll

javascript

提问by Xeoncross

I'm hoping to find a way to get the current viewable window's position (relative to the total page width/height) so I can use it to force a scroll from one section to another. However, there seems to be a tremendous amount of options when it comes to guessing which object holds the true X/Y for your browser.

我希望找到一种方法来获取当前可见窗口的位置(相对于总页面宽度/高度),以便我可以使用它来强制从一个部分滚动到另一个部分。然而,在猜测哪个对象拥有浏览器的真实 X/Y 时,似乎有很多选择。

Which of these do I need to make sure IE 6+, FF 2+, and Chrome/Safari work?

我需要哪些才能确保 IE 6+、FF 2+ 和 Chrome/Safari 正常工作?

window.innerWidth
window.innerHeight
window.pageXOffset
window.pageYOffset
document.documentElement.clientWidth
document.documentElement.clientHeight
document.documentElement.scrollLeft
document.documentElement.scrollTop
document.body.clientWidth
document.body.clientHeight
document.body.scrollLeft
document.body.scrollTop

And are there any others? Once I know where the window is I can set an event chain that will slowly call window.scrollBy(x,y);until it reaches my desired point.

还有其他人吗?一旦我知道窗口在哪里,我就可以设置一个事件链,它会慢慢调用window.scrollBy(x,y);直到到达我想要的点。

回答by thomasrutter

The method jQuery (v1.10) uses to find this is:

jQuery (v1.10) 用来找到这个的方法是:

var doc = document.documentElement;
var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
var top = (window.pageYOffset || doc.scrollTop)  - (doc.clientTop || 0);

That is:

那是:

  • It tests for window.pageXOffsetfirst and uses that if it exists.
  • Otherwise, it uses document.documentElement.scrollLeft.
  • It then subtracts document.documentElement.clientLeftif it exists.
  • window.pageXOffset首先测试并使用它(如果存在)。
  • 否则,它使用document.documentElement.scrollLeft.
  • document.documentElement.clientLeft如果存在,则减去。

The subtraction of document.documentElement.clientLeft/ Toponly appears to be required to correct for situations where you have applied a border (not padding or margin, but actual border) to the root element, and at that, possibly only in certain browsers.

document.documentElement.clientLeft/的减法Top似乎只需要纠正您将边框(不是填充或边距,而是实际边框)应用于根元素的情况,并且可能仅在某些浏览器中。

回答by K-Gun

Maybe more simple;

也许更简单;

var top  = window.pageYOffset || document.documentElement.scrollTop,
    left = window.pageXOffset || document.documentElement.scrollLeft;

Credits: so.dom.js#L492

学分:so.dom.js#L492

回答by Gildas.Tambo

Using pure javascript you can use Window.scrollXand Window.scrollY

使用纯 javascript,您可以使用Window.scrollXWindow.scrollY

window.addEventListener("scroll", function(event) {
    var top = this.scrollY,
        left =this.scrollX;
}, false);

Notes

笔记

The pageXOffset property is an alias for the scrollX property, and The pageYOffset property is an alias for the scrollY property:

pageXOffset 属性是 scrollX 属性的别名, pageYOffset 属性是 scrollY 属性的别名:

window.pageXOffset == window.scrollX; // always true
window.pageYOffset == window.scrollY; // always true

Here is a quick demo

这是一个快速演示

window.addEventListener("scroll", function(event) {
  
    var top = this.scrollY,
        left = this.scrollX;
  
    var horizontalScroll = document.querySelector(".horizontalScroll"),
        verticalScroll = document.querySelector(".verticalScroll");
    
    horizontalScroll.innerHTML = "Scroll X: " + left + "px";
      verticalScroll.innerHTML = "Scroll Y: " + top + "px";
  
}, false);
*{box-sizing: border-box}
:root{height: 200vh;width: 200vw}
.wrapper{
    position: fixed;
    top:20px;
    left:0px;
    width:320px;
    background: black;
    color: green;
    height: 64px;
}
.wrapper div{
    display: inline;
    width: 50%;
    float: left;
    text-align: center;
    line-height: 64px
}
.horizontalScroll{color: orange}
<div class=wrapper>
    <div class=horizontalScroll>Scroll (x,y) to </div>
    <div class=verticalScroll>see me in action</div>
</div>

回答by ali.b.y

function FastScrollUp()
{
     window.scroll(0,0)
};

function FastScrollDown()
{
     $i = document.documentElement.scrollHeight ; 
     window.scroll(0,$i)
};
 var step = 20;
 var h,t;
 var y = 0;
function SmoothScrollUp()
{
     h = document.documentElement.scrollHeight;
     y += step;
     window.scrollBy(0, -step)
     if(y >= h )
       {clearTimeout(t); y = 0; return;}
     t = setTimeout(function(){SmoothScrollUp()},20);

};


function SmoothScrollDown()
{
     h = document.documentElement.scrollHeight;
     y += step;
     window.scrollBy(0, step)
     if(y >= h )
       {clearTimeout(t); y = 0; return;}
     t = setTimeout(function(){SmoothScrollDown()},20);

}