-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmooth-scroll.js
More file actions
168 lines (128 loc) · 5.03 KB
/
Copy pathsmooth-scroll.js
File metadata and controls
168 lines (128 loc) · 5.03 KB
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
/*--------------------------------------------------------------------------
* Smooth Scroller Script, version 1.0.2
* (c) 2007 Dezinerfolio Inc. <midart@gmail.com>
* Edited by @floorish
*
* For details, please check the website : http://dezinerfolio.com/
*
--------------------------------------------------------------------------*/
Scroller = {
// control the speed of the scroller.
// dont change it here directly, please use Scroller.speed=50;
speed:10,
offset:0,
// returns the Y position of the div
gy: function (elem) {
var y = elem.offsetTop;
if (elem.offsetParent) {
while (elem = elem.offsetParent) {
y += elem.offsetTop;
}
}
return y - this.offset;
},
// returns the current scroll position
scrollTop: function () {
var body = document.body;
var d = document.documentElement;
if (body && body.scrollTop) return body.scrollTop;
if (d && d.scrollTop) return d.scrollTop;
if (window.pageYOffset) return window.pageYOffset;
return 0;
},
// attach an event for an element
add: function(elem, type, func) {
if (elem.addEventListener) return elem.addEventListener(type, func, false);
if (elem.attachEvent) return elem.attachEvent('on'+type, func);
},
// kill an event of an element
end: function(e) {
if (window.event) {
window.event.cancelBubble = true;
window.event.returnValue = false;
return;
}
if (e.preventDefault && e.stopPropagation) {
e.preventDefault();
e.stopPropagation();
}
},
// move the viewport to the desired position
scroll: function(targetPos) {
// viewport height
var viewHeight = window.innerHeight || document.documentElement.clientHeight;
// total page height
var pageHeight = document.body.scrollHeight;
// current scroll position
var currentPos = Scroller.scrollTop();
// check if we are at the destination of previous iteration
/*if ( Scroller.previousPos !== false && Scroller.previousPos !== currentPos ) {
return;
}*/
// can't scroll beyond pageHeight: set target to lowest possible
if( pageHeight - targetPos < viewHeight ) {
targetPos = pageHeight - viewHeight;
}
var diff = ( targetPos - currentPos)/Scroller.speed;
// check if target is below current position
if(targetPos > currentPos) {
diff = Math.ceil(diff);
} else {
diff = Math.floor(diff);
}
currentPos += diff;
// scroll to new position
window.scrollTo(0, currentPos);
Scroller.previousPos = currentPos;
// stop if the destination is reached, or this iteration scrolled nothing
if(currentPos == targetPos || diff == 0 ) {
clearInterval(Scroller.interval);
Scroller.previousPos = false;
}
},
// initializer that adds the renderer to the onload function of the window
init: function() {
Scroller.add(window, 'load', Scroller.render);
},
// this method extracts all the anchors and validates then as # and attaches the events.
render: function() {
Scroller.end(this);
var anchors = document.getElementsByTagName('a');
for ( var i = 0; i < anchors.length; i++ ) {
var anchor = anchors[i];
// check if anchor links to something in current page
if( anchor.href && anchor.href.indexOf('#') != -1 &&
anchor.href.indexOf('#') != anchor.href.length-1 &&
((anchor.pathname == location.pathname) || ('/'+anchor.pathname == location.pathname)) ) {
// add event listener
Scroller.add(anchor, 'click',
function() {
Scroller.end(this);
var target = this.getAttribute('href').split("#")[1];
var targetElem = document.getElementById(target);
// check if target exists
if( ! targetElem) {
// check all anchors on the page for matching name attribute
for ( var j=0; j < anchors.length; j++) {
if( anchors[j].name == target ) {
targetElem = anchors[j];
// don't search further
break;
}
}
}
// scroll to target if it exists
if ( targetElem ) {
clearInterval(Scroller.interval);
Scroller.interval = setInterval(function(){
Scroller.scroll(Scroller.gy(targetElem));
}, 10);
}
}
);
}
}
}
};
// invoke the initializer of the scroller
Scroller.init();