gpt4 book ai didi

Javascript 检测浏览器滚动到顶部?

转载 作者:搜寻专家 更新时间:2023-11-01 04:47:22 24 4
gpt4 key购买 nike

如何修改以下代码来检测滚动到首页。

window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
alert(bottom);
}
};

编辑:

顺便说一下,我正在为 Windows Phone 8 开发 IE 10

最佳答案

设法搞清楚了。这是我的代码:

window.onscroll = function() {

var body = document.body; //IE 'quirks'
var document = document.documentElement; //IE with doctype
document = (document.clientHeight) ? document : body;

if (document.scrollTop == 0) {
alert("top");
}
};

检查它是否正在运行:

window.onscroll = function(){

var B = document.body; //IE 'quirks'
var D = document.documentElement; //IE with doctype
D = (D.clientHeight)? D: B;

if (D.scrollTop == 0){
alert("top");
}
};
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

EXPLANATION OF HOW THIS CODE WORKS:

window.onscroll is the command to assign the onscroll event tothe window element.

Now, as the onscroll event gets fired when an element's scrollbaris being scrolled., the element will be the window itself in thiscase.

Now, the function will be called when the event is fired.

In the function, we get the "document.body" to as en IE is the way toget it. After this, we get the documentElement, if there is a doctype.

Then, this line, is the one that chooses between the document or thebody if the document.clientHeight is informed. If it's informed, itwill put document on variable document. If not, it will put the bodyitself.After this, it will check the scrollTop property in order to knowif current scroll position is "at the top"

关于Javascript 检测浏览器滚动到顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16422124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com