-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path平滑锚点.html
129 lines (111 loc) · 3.65 KB
/
平滑锚点.html
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>伪锚点</title>
<style>
.anchor1, .anchor2{width:100px;height:100px;}
.anchor1{background:red;}
.anchor2{background:green;}
.spacing1, .spacing2{height:200px;}
.spacing1{background:yellow;}
.spacing2{background:gray;}
</style>
</head>
<body>
<p>
<a href="#w_anchor1">锚点1</a>
</p>
<p>
<a href="#w_anchor2">锚点2</a>
</p>
<p class="spacing1">间隔1</p>
<p class="spacing2">间隔2</p>
<p class="spacing1">间隔3</p>
<p class="spacing2">间隔4</p>
<p class="spacing1">间隔5</p>
<p class="spacing2">间隔6</p>
<p class="spacing1">间隔7</p>
<p class="spacing2">间隔8</p>
<div id="anchor1" class="anchor1">锚点1</div>
<p class="spacing1">间隔1</p>
<p class="spacing2">间隔2</p>
<p class="spacing1">间隔3</p>
<p class="spacing2">间隔4</p>
<p class="spacing1">间隔5</p>
<p class="spacing2">间隔6</p>
<p class="spacing1">间隔7</p>
<p class="spacing2">间隔8</p>
<div id="anchor2" class="anchor2">锚点2</div>
<p class="spacing1">间隔1</p>
<p class="spacing2">间隔2</p>
<p class="spacing1">间隔3</p>
<p class="spacing2">间隔4</p>
<p class="spacing1">间隔5</p>
<p class="spacing2">间隔6</p>
<p class="spacing1">间隔7</p>
<p class="spacing2">间隔8</p>
<script>
(function(window, undefined){
// 监听页面加载完成后,检查是否需要定位锚点
window.onload = function(){
scrollToAnchor();
};
// 监听地址栏url的hash值改变时,检查是否需要定位锚点
window.onhashchange = function(){
scrollToAnchor();
};
// 滚动到自定义的伪锚点
function scrollToAnchor(){
var hash = getHash(), // 获取url的hash值
anchor = getAnchor(hash), // 获取伪锚点的id
anchorDom, // 伪锚点dom对象
anchorScrollTop; // 伪锚点距离页面顶部的距离
// 如果不存在伪锚点,则直接结束
if(anchor.length < 1){
return;
}
anchorDom = getDom(anchor);
anchorScrollTop = anchorDom.offsetTop;
animationToAnchor(document.documentElement.scrollTop, anchorScrollTop);
}
/*
@function 滚动到指定位置方法
@param startNum {int} -- 开始位置
@param stopNum {int} -- 结束位置
*/
function animationToAnchor(startNum, stopNum){
var nowNum = startNum + 50; // 步进为10
if(nowNum > stopNum){
nowNum = stopNum;
}
// 缓动方法
window.requestAnimationFrame(function(){
document.documentElement.scrollTop = nowNum; // 当前示例页面,滚动条在body,所以滚动body
// 滚动到预定位置则结束
if(nowNum == stopNum){
return;
}
animationToAnchor(nowNum, stopNum); // 只要还符合缓动条件,则递归调用
});
}
// 获取锚点id
function getAnchor(str){
return checkAnchor(str) ? str.split("w_")[1] : "";
}
// 判断是否为特殊的hash值,也即是否为伪锚点
function checkAnchor(str){
return str.indexOf("w_") == 0 ? true : false;
}
// 获取hash值
function getHash(){
return window.location.hash.substring(1);
}
// 获取dom对象
function getDom(id){
return document.getElementById(id);
}
})(window);
</script>
</body>
</html>