Skip to content

Commit eeff7de

Browse files
committed
Initial commit
0 parents  commit eeff7de

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bower_components
2+

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Dustan Kasten
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
smoothScrollTo.js
2+
=================
3+
4+
Polyfill for window.scrollTo(x, y, 'smooth');

bower.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "smoothScrollTo",
3+
"version": "0.0.0",
4+
"main": "smoothScrollTo.js",
5+
"ignore": [
6+
"**/.*",
7+
"node_modules",
8+
"bower_components",
9+
"test",
10+
"tests"
11+
]
12+
}

smoothScrollTo.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(function () {
2+
'use strict';
3+
4+
if ('scrollBehavior' in document.documentElement.style) return;
5+
6+
var originalScrollTo = window.scrollTo;
7+
8+
function now() {
9+
return window.performance !== undefined && window.performance.now !== undefined ? window.performance.now() : Date.now !== undefined ? Date.now() : +new Date;
10+
}
11+
12+
// ease-in-out
13+
function ease(k) {
14+
return 0.5 * (1 - Math.cos(Math.PI * k));
15+
}
16+
17+
window.scrollTo = function(x, y, type) {
18+
if (type !== 'smooth')
19+
return originalScrollTo(x, y);
20+
21+
// TODO: make this intelligent. 300ms per scroll
22+
var SCROLL_TIME = 300;
23+
var frame;
24+
var sx = this.pageXOffset;
25+
var sy = this.pageYOffset;
26+
27+
var startTime = now();
28+
29+
// TODO: look into polyfilling scroll-behavior: smooth css property
30+
// var ey = element.offsetTop, ex = element.offsetLeft;
31+
var step = function() {
32+
var time = now();
33+
var elapsed = (time - startTime) / SCROLL_TIME;
34+
elapsed = elapsed > 1 ? 1 : elapsed;
35+
36+
var value = ease(elapsed);
37+
var _x = sx + ( x - sx ) * value;
38+
var _y = sy + ( y - sy ) * value;
39+
40+
if (_x >= x) _x = x;
41+
if (_y >= y) _y = y;
42+
window.scrollTo(_x, _y)
43+
44+
if (_x === x && _y === y) return;
45+
frame = requestAnimationFrame(step);
46+
}
47+
48+
frame = requestAnimationFrame(step);
49+
}
50+
51+
}());
52+

test.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
</head>
5+
<body>
6+
<h1>Quick and dirty scrollTo(x, y, 'smooth') testing</h1>
7+
<p style='height: 500px'>For real. Really quick. Really dirty.</p>
8+
<h2>A second after pageload, you should be smoothly scrolled to my neighborhood</h2>
9+
<p style='height: 500px'>Welcome to the neighborhood.</p>
10+
11+
<script src="smoothScrollTo.js"></script>
12+
<script>
13+
document.addEventListener('DOMContentLoaded', function(e) {
14+
setTimeout(function() {
15+
window.scrollTo(0, 500, 'smooth');
16+
}, 1000);
17+
}, false);
18+
</script>
19+
</body>
20+
</html>
21+

0 commit comments

Comments
 (0)