如何在不滚动的情况下到达页面顶部
How to get to the top of page without scrolling
这个问题困扰了我很久:我想让页面不滚动到顶部
我试过了,但没用:
window.scrollTo({
top:0,
behavior: 'auto'
});
你需要 x-coord
和 y-coord
试试这个
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
});
请尝试一个简单的解决方案。
只需在此处为此替换您的代码:
window.scroll(0, 0);
干杯,
马塞洛
如果您的目标是避免实际滚动(“动画”),请使用 behavior: 'instant'
。
const button = document.getElementById('instant-top-button')
button.addEventListener('click', () => window.scrollTo({
top: 0,
behavior: 'instant',
}))
<p>scroll down ⭳</p>
<p>
<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/>
</p>
<button id="instant-top-button">⭱ to top, instantly!</button>
这个问题困扰了我很久:我想让页面不滚动到顶部
我试过了,但没用:
window.scrollTo({
top:0,
behavior: 'auto'
});
你需要 x-coord
和 y-coord
试试这个
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
});
请尝试一个简单的解决方案。 只需在此处为此替换您的代码:
window.scroll(0, 0);
干杯, 马塞洛
如果您的目标是避免实际滚动(“动画”),请使用 behavior: 'instant'
。
const button = document.getElementById('instant-top-button')
button.addEventListener('click', () => window.scrollTo({
top: 0,
behavior: 'instant',
}))
<p>scroll down ⭳</p>
<p>
<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/>
</p>
<button id="instant-top-button">⭱ to top, instantly!</button>