使用 JavaScript 滚动到页面顶部? [英] Scroll to the top of the page using JavaScript?

查看:27
本文介绍了使用 JavaScript 滚动到页面顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 JavaScript 滚动到页面顶部?滚动条立即跳到页面顶部也是可取的,因为我不希望实现平滑滚动.

How do I scroll to the top of the page using JavaScript? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.

推荐答案

如果您不需要更改动画,那么您不需要使用任何特殊插件 - 我只使用原生 JavaScript window.scrollTo() 方法 -- 传入 0, 0 会立即将页面滚动到左上角.

If you don't need the change to animate then you don't need to use any special plugins - I'd just use the native JavaScript window.scrollTo() method -- passing in 0, 0 will scroll the page to the top left instantly.

window.scrollTo(xCoord, yCoord);

参数

  • xCoord 是沿水平轴的像素.
  • yCoord 是沿垂直轴的像素.

这篇关于使用 JavaScript 滚动到页面顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆