Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript滚动到页面顶部吗?(Scroll to the top of the page using JavaScript?)

How do I scroll to the top of the page using a JavaScript?

(如何使用JavaScript滚动到页面顶部?)

It is desirable even if the scrollbar instantly jumps to the top.

(即使滚动条立即跳到顶部也是可取的。)

I'm not looking for a smooth scrolling.

(我不是在寻找平滑的滚动。)

  ask by KingNestor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(如果您不需要更改以进行动画处理,则无需使用任何特殊的插件-我只使用本机JavaScript window.scrollTo方法-传入0,0即可将页面立即滚动到左上角。)

window.scrollTo(x-coord, y-coord);

Parameters

(参量)

  • x-coord is the pixel along the horizontal axis.

    (x坐标是沿水平轴的像素。)

  • y-coord is the pixel along the vertical axis.

    (y坐标是沿垂直轴的像素。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...