This repository has been archived by the owner on May 15, 2024. It is now read-only.
forked from iamdustan/smoothscroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
338 lines (288 loc) · 11.1 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smooth scroll polyfill</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
<link href='https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700' rel='stylesheet' type='text/css'>
<!-- rAF polyfill -->
<script>
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
</script>
<!-- smooth scroll behavior polyfill -->
<script src="src/smoothscroll.js"></script>
<!-- site styles -->
<style>
body {
background-color: #fefefe;
color: #212121;
font-family: 'Roboto Condensed', Arial, sans-serif;
font-size: 20px;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.container {
margin: 0 auto;
max-width: 800px;
padding: 40px;
overflow: hidden;
position: relative;
}
a {
color: #f44336;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
header {
background-color: #f44336;
color: #fefefe;
}
.featured {
background-color: #efefef;
}
.featured__link {
display: inline-block;
margin-right: 45px;
}
h1 {
font-size: 2.75em;
font-weight: 400;
letter-spacing: -.03em;
line-height: 1em;
}
p {
line-height: 1.5em;
}
small {
font-weight: 400;
}
code {
font-family: Menlo, monospace;
font-size: 15px;
vertical-align: middle;
}
pre {
overflow: auto;
width: 100%;
}
pre code {
font-size: 14px;
}
.actions {
margin-top: 30px;
}
.btn {
background-color: #f44336;
border-width: 0;
border-radius: 4px;
color: #fefefe;
cursor: pointer;
font-weight: 700;
padding: 6px 12px;
text-transform: uppercase;
transition: all .35s ease;
}
.btn:active {
background-color: #d32f2f;
}
.scrollable-parent {
background-color: #efefef;
border-radius: 4px;
margin: 20px 0 0;
max-height: 200px;
overflow: scroll;
padding: 30px 50px;
}
.hello {
text-align: center;
}
footer {
background-color: #404040;
color: #fefefe;
font-size: 18px;
}
footer a {
color: inherit;
}
</style>
</head>
<body>
<header>
<div class="container">
<h1>smooth scroll polyfill</h1>
</div>
</header>
<main class="featured">
<div class="container">
<p>The <strong>Scroll Behavior</strong> specification has been introduced as an extension of the <strong>Window</strong> interface to allow for the developer to opt in to native smooth scrolling.</p>
<p>Check the repository on <a href="https://github.com/iamdustan/smoothscroll">GitHub</a> or download the polyfill as an <a href="https://npmjs.org/smoothscroll-polyfill">npm module</a>.</p>
</div>
</main>
<section class="sample sample-scrollToBottom">
<div class="container">
<h2>window.scroll <small>or</small> window.scrollTo</h2>
<pre><code>window.scroll({ top: 2500, left: 0, behavior: 'smooth' });</code></pre>
<div class="actions">
<button class="btn js-scroll-to-bottom">scroll to bottom</button>
</div>
</div>
</section>
<section class="sample sample-scrollBy">
<div class="container">
<h2>window.scrollBy</h2>
<pre><code>window.scrollBy({ top: 100, left: 0, behavior: 'smooth' });</code></pre>
<div class="actions">
<button class="btn js-scroll-by">scroll by 100 pixels</button>
</div>
</div>
</section>
<section class="sample sample-scrollBack">
<div class="container">
<h2>window.scrollBy</h2>
<pre><code>window.scrollBy({ top: -100, left: 0, behavior: 'smooth' });</code></pre>
<div class="actions">
<button class="btn js-scroll-back">scroll back 100 pixels</button>
</div>
</div>
</section>
<section class=" sample sample--scrollIntoView">
<div class="container">
<h2>element.scrollIntoView</h2>
<pre><code>document.querySelector('.hello').scrollIntoView({ behavior: 'smooth' });</code></pre>
<div class="actions">
<button class="btn js-scroll-into-hello">Scroll into view</button>
</div>
<article class="scrollable-parent">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui iure obcaecati, repudiandae aspernatur cumque recusandae
adipisci consequuntur maiores, quo in nulla ratione facere distinctio beatae, quae consequatur ab labore dolorum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui iure obcaecati, repudiandae aspernatur cumque recusandae
adipisci consequuntur maiores, quo in nulla ratione facere distinctio beatae, quae consequatur ab labore dolorum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui iure obcaecati, repudiandae aspernatur cumque recusandae
adipisci consequuntur maiores, quo in nulla ratione facere distinctio beatae, quae consequatur ab labore dolorum.</p>
<h3 class="hello"><em>hello!</em></h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui iure obcaecati, repudiandae aspernatur cumque recusandae
adipisci consequuntur maiores, quo in nulla ratione facere distinctio beatae, quae consequatur ab labore dolorum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui iure obcaecati, repudiandae aspernatur cumque recusandae
adipisci consequuntur maiores, quo in nulla ratione facere distinctio beatae, quae consequatur ab labore dolorum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui iure obcaecati, repudiandae aspernatur cumque recusandae
adipisci consequuntur maiores, quo in nulla ratione facere distinctio beatae, quae consequatur ab labore dolorum.</p>
</article>
</div>
</section>
<section class="sample sample-elementScrollToTop">
<div class="container">
<h2>element.scroll <small>or</small> element.scrollTo</h2>
<pre><code>element.scrollTo({ top: 0, left: 0, behavior: 'smooth' });</code></pre>
<div class="actions">
<button class="btn js-elementScrollToTop">scroll element to top</button>
</div>
</div>
</section>
<section class="sample sample-scrollBy2">
<div class="container">
<h2>element.scrollBy</h2>
<pre><code>element.scrollBy({ top: 100, left: 0, behavior: 'smooth' });</code></pre>
<div class="actions">
<button class="btn js-scroll-by2">scroll element by 100 pixels</button>
</div>
</div>
</section>
<section class="sample sample-scrollToTop">
<div class="container">
<h2>Scroll to top</h2>
<pre><code>window.scroll({ top: 0, left: 0, behavior: 'smooth' });</code></pre>
<p><strong>or</strong></p>
<pre><code>document.querySelector('header').scrollIntoView({ behavior: 'smooth' });</code></pre>
<div class="actions">
<button class="btn js-scroll-to-top">scroll to top</button>
</div>
</div>
</section>
<section>
<div class="container"></div>
</section>
<section class="featured">
<div class="container">
<a class="featured__link" href="https://github.com/iamdustan/smoothscroll">Polyfill on <strong>GitHub</strong></a>
<a class="featured__link" href="https://github.com/iamdustan/smoothscroll-polyfill">Polyfill on <strong>npm</strong></a>
<a class="featured__link" href="https://drafts.csswg.org/cssom-view/#extensions-to-the-window-interface">Draft on <strong>W3C</strong></a>
<a class="featured__link" href="https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior">Documentation on <strong>MDN</strong></a>
</div>
</section>
<footer>
<div class="container">
<p>Polyfill written by <a href="https://twitter.com/iamdustan">Dustan Kasten</a> and <a href="https://twitter.com/jeremenichelli">Jeremias Menichelli</a></p>
<p>All rights reserved © <a href="https://iamdustan.github.io/smoothscroll">https://iamdustan.github.io/smoothscroll</a></p>
</div>
</footer>
<script>
// add event listener on load
window.addEventListener('load', function() {
// scroll to bottom
document.querySelector('.js-scroll-to-bottom').addEventListener('click', function(e) {
e.preventDefault();
window.scrollTo({ top: document.body.clientHeight - window.innerHeight, left: 0, behavior: 'smooth' });
});
// scroll to top
document.querySelector('.js-scroll-to-top').addEventListener('click', function(e) {
e.preventDefault();
document.querySelector('header').scrollIntoView({ behavior: 'smooth' });
});
// scroll by
document.querySelector('.js-scroll-by').addEventListener('click', function(e) {
e.preventDefault();
window.scrollBy({ top: 100, left: 0, behavior: 'smooth' });
});
// scroll back
document.querySelector('.js-scroll-back').addEventListener('click', function(e) {
e.preventDefault();
window.scrollBy({ top: -100, left: 0, behavior: 'smooth' });
});
// scroll into view
document.querySelector('.js-scroll-into-hello').addEventListener('click', function(e) {
e.preventDefault();
document.querySelector('.hello').scrollIntoView({ behavior: 'smooth' });
});
// element scroll to
document.querySelector('.js-elementScrollToTop').addEventListener('click', function(e) {
e.preventDefault();
document.querySelector('.scrollable-parent').scroll({ top: 0, left: 0, behavior: 'smooth' });
});
// element scroll by
document.querySelector('.js-scroll-by2').addEventListener('click', function(e) {
e.preventDefault();
document.querySelector('.scrollable-parent').scrollBy({ top: 100, left: 0, behavior: 'smooth' });
});
});
</script>
</body>
</html>