-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
164 lines (119 loc) · 4.73 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="scrollspy_style.css" rel="stylesheet" type="text/css">
<script>
function scrollspy () {
let i = 0;
let doc = document.getElementById("demo");
let mynav = document.getElementById("mynav");
let linklist = mynav.getElementsByTagName("A");
let link = "";
let hashpositions = [];
let position = [];
for (i = 0; i < linklist.length; i++) {
link = linklist[i].href;
if (link.indexOf("#") > -1) {
position[position.length] = document.getElementById(link.split("#")[1]);
hashpositions[hashpositions.length] = position[i].offsetTop;
linklist[i].innerHTML = linklist[i].innerHTML + " " + position[i].offsetTop
+ " " + (hashpositions[i] + position[i].clientHeight);
}
}
hashpositions.sort(function(a, b){return a-b});
return function () {
let currentposition = window.pageYOffset;
doc.innerHTML = currentposition;
for (i = 0; i < hashpositions.length; i++) {
if (currentposition + 10 >= hashpositions[i]) {
linklist[i].parentNode.classList.add("myactive");
}
else {
linklist[i].parentNode.classList.remove("myactive");
}
if (currentposition > (hashpositions[i] + position[i].clientHeight)) {
linklist[i].parentNode.classList.remove("myactive");
}
}
}
}
function scrollsmooth_run (event) {
event.preventDefault();
let myHash = this.hash;
let mysection = document.getElementById(this.hash.substring(1));
let pageYOffset_start = window.pageYOffset;
let pos_diff = mysection.offsetTop - pageYOffset_start;
let pos_diff_delta = pos_diff / 100;
let varCounter = 0;
let id = setInterval(frame, 5);
function frame() {
console.log((mysection.offsetTop - 10) + " < " + window.pageYOffset + " < " + (mysection.offsetTop + 10));
let test = (mysection.offsetTop - 10) < window.pageYOffset && window.pageYOffset < (mysection.offsetTop + 10);
console.log("7: " + test);
if(varCounter <= 110) {
varCounter++;
} else {
clearInterval(id);
}
if ((mysection.offsetTop - 5) < window.pageYOffset && window.pageYOffset < (mysection.offsetTop + 5)) {
clearInterval(id);
} else {
window.scrollTo(window.pageXOffset, window.pageYOffset + pos_diff_delta);
}
}
}
function scrollsmooth () {
let linklist = mynav.getElementsByTagName("A");
for (i = 0; i < linklist.length; i++) {
linklist[i].onclick = scrollsmooth_run;
}
}
document.addEventListener('DOMContentLoaded', function() {
let mybody = document.getElementById("mybody");
mybody.onscroll = scrollspy();
scrollsmooth();
}, false);
</script>
<title>scrollspy</title>
</head>
<body id="mybody">
<header id="myhead">
<h1> this is the header </h1>
</header>
<section id="mytable">
<section id="content">
<section id="left-side">
<nav id="mynav">
<p id="demo">test</p>
<ul>
<li><a href="#section1">section 1</a></li>
<li><a href="#section2">section 2</a></li>
<li><a href="#section3">section 3</a></li>
<li><a href="#section4">section 4</a></li>
</ul>
</nav>
</section>
<section id="right-side">
<section id="section1" class="section">
<h2> section 1 </h2>
<p> text </p>
</section>
<section id="section2" class="section">
<h2> section 2 </h2>
<p> text </p>
</section>
<section id="section3" class="section">
<h2> section 3 </h2>
<p> text </p>
</section>
<section id="section4" class="section">
<h2> section 4 </h2>
<p> text </p>
</section>
</section>
</section>
</section>
<footer id="myfooter"> this is the footer </footer>
</body>
</html>