javascript 为什么要引入 window.scrollY 和 window.scrollX?

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

Why were window.scrollY and window.scrollX introduced?

javascriptdompropertiescross-browsercssom

提问by Knu

As far as I know, pageXOffset/pageYOffsetproperties were already available since Netscape 4 era.
And it seems scrollX/scrollYwere introduced circa Netscape 6.

据我所知,pageXOffset/pageYOffset自 Netscape 4 时代以来,属性已经可用。
它似乎scrollX/scrollY是在 Netscape 6 左右引入的。

Alternative question:

替代问题:

Q2. Is there a browser which implements scrollX/scrollY but doesn't support pageXOffset/pageYOffset?

Q2。是否有实现 scrollX/scrollY 但不支持 pageXOffset/pageYOffset 的浏览器?

I will add a third question because no one was able to answer the previous ones:

我将添加第三个问题,因为没有人能够回答之前的问题:

Q3. scrollX/scrollY was added to the latest editor's draft of the CCSOM and the working draft only got pageXOffset/pageYOffset, why are they keeping both attributes?

Q3。最新的CCSOM编辑草稿中加入了scrollX/scrollY,工作草稿只有pageXOffset/pageYOffset,为什么还要保留这两个属性?

回答by gblazex

Is there a browser which implements scrollY/X but doesn't support pageY/XOffset

是否有实现 scrollY/X 但不支持 pageY/XOffset 的浏览器

I guess what you want to know is whether you can fully trust pageY/XOffsetand leave scrollY/Xout of the game. The answer is yes. pageY/XOffsetis working in Firefox, Chrome, Opera, and IE 9!

我想您想知道的是您是否可以完全信任pageY/XOffset并退出scrollY/X游戏。答案是肯定的。pageY/XOffset正在 Firefox、Chrome、Opera 和IE 9 中工作!

I can't test scrollX/Yon IE9 currently, but it is not listed on MSDN propertiesso there is a good chance it answers your question. So there may be browsers implementing pageY/XOffsetbut not scrollY/X.

scrollX/Y目前无法在 IE9 上进行测试,但它未列在MSDN 属性中,因此很有可能回答您的问题。所以可能有浏览器实现pageY/XOffset但没有scrollY/X.

Why were window.scrollY and window.scrollX introduced?

为什么要引入 window.scrollY 和 window.scrollX?

As scrollYis only an alias, I'm sure it is only for better readability.

由于scrollY只是别名,我确定它只是为了更好的可读性。

回答by pepkin88

Everything about it.As you can see pageXOffsetisn't supported by Internet Explorer 8 and below. In IE8 (and lower) you should use scrollLeft/ scrollTopof document.bodyor document.documentElementdepending what works (just like it is implemented in jQuery).

关于它的一切。如您所见pageXOffset,Internet Explorer 8 及以下版本不支持。在 IE8(及更低版本)中,您应该使用scrollLeft/ scrollTopofdocument.bodydocument.documentElement取决于什么工作(就像它在 jQuery 中实现一样)。

You can check it here: http://jsfiddle.net/8RFAn/1/

你可以在这里查看:http: //jsfiddle.net/8RFAn/1/

And I don't know why window.scrollX/Ywere introduced, for me these properties looks like pageX/YOffsetin terms of effect and browsers in which are implemented.

我不知道为什么要window.scrollX/Y引入,对我来说,这些属性pageX/YOffset在效果和浏览器方面看起来像在其中实现。

回答by Azmisov

As pepkin88 said, pageXOffset (and all pageXY values) are not supported in earlier versions of Internet Explorer. Most of the properties used to locate objects and calculate mouse positions are not part of the W3C standards. From what I remember reading, most of the properties are Microsoft's invention. So when you speak of scrollX and scrollY, you are speaking of the originalpageXYOffset values. When standard compliant browsers came along that supported the pageXY properties (not part of Microsoft's model), they decided it would be too confusing to stick with the scrollXY values. So, they switched to a much more logical (in their opinion) version: pageOffset.

正如 pepkin88 所说,早期版本的 Internet Explorer 不支持 pageXOffset(和所有 pageXY 值)。大多数用于定位对象和计算鼠标位置的属性不是 W3C 标准的一部分。从我记得阅读的内容来看,大多数属性都是微软的发明。因此,当您谈到 scrollX 和 scrollY 时,您指的是原始pageXYOffset 值。当支持 pageXY 属性(不是 Microsoft 模型的一部分)的标准兼容浏览器出现时,他们认为坚持使用 scrollXY 值会太混乱。因此,他们切换到了一个更合乎逻辑的(在他们看来)版本:pageOffset。

In my opinion, I find the scrollXY values more intuitive to use. I will usually have to pause and think, every time I try to decide between screenX, pageX, or clientX. I think they could have come up with more intuitive names for each of the properties. Anyways, none of them are really W3C standard, so I guess conventions aren't really that important in this realm of JavaScript. I'm very glad Microsoft invented them though, because they turn out to be very useful.

在我看来,我发现 scrollXY 值使用起来更直观。每次我尝试在 screenX、pageX 或 clientX 之间做出决定时,我通常都必须停下来思考。我认为他们可以为每个属性想出更直观的名称。无论如何,它们都不是真正的 W3C 标准,所以我想约定在 JavaScript 的这个领域并不是那么重要。我很高兴微软发明了它们,因为它们非常有用。

回答by Shadow Wizard is Ear For You

I will try to answer using my common sense, nothing official so sorry if that's not what you meant in your question.

我会尽量用我的常识来回答,如果这不是你在问题中的意思,没有什么正式的那么抱歉。

window.scrollY and window.scrollX are used to decide how much the website user has scrolled vertically or horizontally.

window.scrollY 和 window.scrollX 用于决定网站用户垂直或水平滚动了多少。

This is useful for example in scripts that can only detect the "absolute" position of something in the document and need to place something else on top/beside of it, so using the scrollX and scrollY values they can calculate the proper position.

例如,这在只能检测文档中某些内容的“绝对”位置并且需要在其顶部/旁边放置其他内容的脚本中非常有用,因此使用 scrollX 和 scrollY 值可以计算出正确的位置。

So probably when demand raised for dynamic scripts like custom tooltip, the people responsible for the JavaScript "framework" answered the call and introduced those properties.

因此,当对自定义工具提示等动态脚本的需求增加时,负责 JavaScript“框架”的人员可能会接听电话并引入这些属性。

Like I said, not official answer, your Googling will be as good as mine to find such answer. :)

就像我说的,不是官方答案,你的谷歌搜索会和我的一样好找到这样的答案。:)