Window scrollTo() 方法

实例

将文档滚动到水平位置 "500" :

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. width: 5000px;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <p>单击按钮将文档窗口水平滚动至 500 像素。</p>
  12. <button onclick="scrollWin()">点击水平滚动!</button><br><br>
  13. <script>
  14. function scrollWin() {
  15. window.scrollTo(500, 0);
  16. }
  17. </script>
  18. </body>
  19. </html>

定义与用法

scrollTo() 方法将文档滚动到指定的坐标。

提示: 使用 scrollBy() 方法多次滚动指定距离。


浏览器支持

方法
scrollTo()YesYesYesYesYes

语法

  1. window.scrollTo(xpos, ypos)

参数值

参数Type描述
xposNumber必填。要沿 x 轴(水平)滚动到的坐标,以像素为单位
yposNumber必填。要沿 y 轴(垂直)滚动到的坐标,以像素为单位

技术细节

返回值:无返回值

更多实例

实例

将文档水平滚动至 "300",垂直滚动至 "500" :

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. width: 5000px;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <p>单击按钮可水平滚动文档窗口 300 像素,垂直滚动 500 像素。</p>
  12. <button onclick="scrollWin()">点击滚动</button>
  13. <br>
  14. <br>
  15. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  16. <br>
  17. <br>
  18. <br>
  19. <br>
  20. <br>
  21. <br>
  22. <br>
  23. <br>
  24. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  25. <br>
  26. <br>
  27. <br>
  28. <br>
  29. <br>
  30. <br>
  31. <br>
  32. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  33. <br>
  34. <br>
  35. <br>
  36. <br>
  37. <br>
  38. <br>
  39. <br>
  40. <br>
  41. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  42. <br>
  43. <br>
  44. <br>
  45. <br>
  46. <br>
  47. <br>
  48. <br>
  49. <br>
  50. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  51. <br>
  52. <br>
  53. <br>
  54. <br>
  55. <br>
  56. <br>
  57. <br>
  58. <br>
  59. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  60. <br>
  61. <br>
  62. <br>
  63. <br>
  64. <br>
  65. <br>
  66. <br>
  67. <br>
  68. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  69. <br>
  70. <br>
  71. <br>
  72. <br>
  73. <br>
  74. <br>
  75. <br>
  76. <br>
  77. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  78. <br>
  79. <br>
  80. <br>
  81. <br>
  82. <br>
  83. <br>
  84. <br>
  85. <br>
  86. <p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
  87. <br>
  88. <br>
  89. <br>
  90. <br>
  91. <br>
  92. <br>
  93. <br>
  94. <br>
  95. <script>
  96. function scrollWin() {
  97. window.scrollTo(300, 500);
  98. }
  99. </script>
  100. </body>
  101. </html>

相关页面

Window 对象: scrollBy() 方法

分类导航