JavaScript中的window.scrollTo


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/04/15/JavaScript%E4%B8%AD%E7%9A%84window-scrollTo/

摘要

本文主要讲述了:

  1. 作用
  2. 参数

正文

作用

滚动到页面,与scroll等效

示例:点击屏幕,滚动到left=0, top=2000的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<script>
window.addEventListener('click', function () {
scrollTo(0, 2000);
});
</script>
</body>
</html>

示例:点击屏幕,平滑滚动到left=0, top=2000的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<h1>jsweibo.github.io</h1>
<script>
window.addEventListener('click', function () {
scrollTo({
left: 0,
top: 2000,
behavior: 'smooth',
});
});
</script>
</body>
</html>

参数

scrollTo()的参数有两种规格:一种是传递 2 个数值(X 坐标、Y 坐标),另一种是传递 1 个ScrollToOptions实例

left

表示距离页面左侧的距离,即 X 坐标

top

表示距离页面顶部的距离,即 Y 坐标

ScrollToOptions

滚动到选项对象

包含 3 个属性,分别是lefttopbehavior

behavior

表示滚动效果

取值范围为:

  • smooth Firefox 支持,但 Chrome 和 Edg 不支持
  • auto

默认值为auto

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/04/15/JavaScript%E4%B8%AD%E7%9A%84window-scrollTo/


本文对你有帮助?请支持我


支付宝
微信