gpt4 book ai didi

javascript - 如何在移动设备中滚动到页面顶部

转载 作者:行者123 更新时间:2023-11-28 06:26:38 25 4
gpt4 key购买 nike

我在 onLoad 中使用以下代码转到页面顶部。Javascript 中的 window.scrollTo(0,0) 它适用于浏览器,但不适用于任何移动设备。

最佳答案

有很多关于移动页面的“window.scrollTo(0,1) 修复”的文章。使用魔数(Magic Number)来设置超时可能会让用户感到沮丧。内容开始出现,用户开始向下滚动页面并消费信息,然后快速返回到页面顶部。更好的方法是这样的:

window.addEventListener("load", function() {
setTimeout(function() {
var scrollPos = window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop;

// seems like the author of linked code source had a logic bug here
// if you need to apply this, probably you will want to check scrollPos > 1

// I keep the comment above - although it is wrong. You should
// not scroll, once the user already interacted with the page.
// For further information, see the linked article below.
if (scrollPos < 1) {
window.scrollTo(0,1);
}
}, 0);
});

了解更多信息 - here is one article (脚本来自那里)

关于javascript - 如何在移动设备中滚动到页面顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072707/

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