-
Notifications
You must be signed in to change notification settings - Fork 1
/
zenscroll.js
357 lines (317 loc) · 12.3 KB
/
zenscroll.js
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* Zenscroll 4.0.2
* https://github.com/zengabor/zenscroll/
*
* Copyright 2015–2018 Gabor Lenard
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org>
*
*/
/*jshint devel:true, asi:true */
/*global define, module */
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory())
} else if (typeof module === "object" && module.exports) {
module.exports = factory()
} else {
(function install() {
// To make sure Zenscroll can be referenced from the header, before `body` is available
if (document && document.body) {
root.zenscroll = factory()
} else {
// retry 9ms later
setTimeout(install, 9)
}
})()
}
}(this, function () {
"use strict"
// Detect if the browser already supports native smooth scrolling (e.g., Firefox 36+ and Chrome 49+) and it is enabled:
var isNativeSmoothScrollEnabledOn = function (elem) {
return elem && "getComputedStyle" in window &&
window.getComputedStyle(elem)["scroll-behavior"] === "smooth"
}
// Exit if it’s not a browser environment:
if (typeof window === "undefined" || !("document" in window)) {
return {}
}
var makeScroller = function (container, defaultDuration, edgeOffset) {
// Use defaults if not provided
defaultDuration = defaultDuration || 999 //ms
if (!edgeOffset && edgeOffset !== 0) {
// When scrolling, this amount of distance is kept from the edges of the container:
edgeOffset = 9 //px
}
// Handling the life-cycle of the scroller
var scrollTimeoutId
var setScrollTimeoutId = function (newValue) {
scrollTimeoutId = newValue
}
/**
* Stop the current smooth scroll operation immediately
*/
var stopScroll = function () {
clearTimeout(scrollTimeoutId)
setScrollTimeoutId(0)
}
var getTopWithEdgeOffset = function (elem) {
return Math.max(0, container.getTopOf(elem) - edgeOffset)
}
/**
* Scrolls to a specific vertical position in the document.
*
* @param {targetY} The vertical position within the document.
* @param {duration} Optionally the duration of the scroll operation.
* If not provided the default duration is used.
* @param {onDone} An optional callback function to be invoked once the scroll finished.
*/
var scrollToY = function (targetY, duration, onDone) {
stopScroll()
if (duration === 0 || (duration && duration < 0) || isNativeSmoothScrollEnabledOn(container.body)) {
container.toY(targetY)
if (onDone) {
onDone()
}
} else {
var startY = container.getY()
var distance = Math.max(0, targetY) - startY
var startTime = new Date().getTime()
duration = duration || Math.min(Math.abs(distance), defaultDuration);
(function loopScroll() {
setScrollTimeoutId(setTimeout(function () {
// Calculate percentage:
var p = Math.min(1, (new Date().getTime() - startTime) / duration)
// Calculate the absolute vertical position:
var y = Math.max(0, Math.floor(startY + distance*(p < 0.5 ? 2*p*p : p*(4 - p*2)-1)))
container.toY(y)
if (p < 1 && (container.getHeight() + y) < container.body.scrollHeight) {
loopScroll()
} else {
setTimeout(stopScroll, 99) // with cooldown time
if (onDone) {
onDone()
}
}
}, 9))
})()
}
}
/**
* Scrolls to the top of a specific element.
*
* @param {elem} The element to scroll to.
* @param {duration} Optionally the duration of the scroll operation.
* @param {onDone} An optional callback function to be invoked once the scroll finished.
*/
var scrollToElem = function (elem, duration, onDone) {
scrollToY(getTopWithEdgeOffset(elem), duration, onDone)
}
/**
* Scrolls an element into view if necessary.
*
* @param {elem} The element.
* @param {duration} Optionally the duration of the scroll operation.
* @param {onDone} An optional callback function to be invoked once the scroll finished.
*/
var scrollIntoView = function (elem, duration, onDone) {
var elemHeight = elem.getBoundingClientRect().height
var elemBottom = container.getTopOf(elem) + elemHeight
var containerHeight = container.getHeight()
var y = container.getY()
var containerBottom = y + containerHeight
if (getTopWithEdgeOffset(elem) < y || (elemHeight + edgeOffset) > containerHeight) {
// Element is clipped at top or is higher than screen.
scrollToElem(elem, duration, onDone)
} else if ((elemBottom + edgeOffset) > containerBottom) {
// Element is clipped at the bottom.
scrollToY(elemBottom - containerHeight + edgeOffset, duration, onDone)
} else if (onDone) {
onDone()
}
}
/**
* Scrolls to the center of an element.
*
* @param {elem} The element.
* @param {duration} Optionally the duration of the scroll operation.
* @param {offset} Optionally the offset of the top of the element from the center of the screen.
* A value of 0 is ignored.
* @param {onDone} An optional callback function to be invoked once the scroll finished.
*/
var scrollToCenterOf = function (elem, duration, offset, onDone) {
scrollToY(Math.max(0, container.getTopOf(elem) - container.getHeight()/2 + (offset || elem.getBoundingClientRect().height/2)), duration, onDone)
}
/**
* Changes default settings for this scroller.
*
* @param {newDefaultDuration} Optionally a new value for default duration, used for each scroll method by default.
* Ignored if null or undefined.
* @param {newEdgeOffset} Optionally a new value for the edge offset, used by each scroll method by default. Ignored if null or undefined.
* @returns An object with the current values.
*/
var setup = function (newDefaultDuration, newEdgeOffset) {
if (newDefaultDuration === 0 || newDefaultDuration) {
defaultDuration = newDefaultDuration
}
if (newEdgeOffset === 0 || newEdgeOffset) {
edgeOffset = newEdgeOffset
}
return {
defaultDuration: defaultDuration,
edgeOffset: edgeOffset
}
}
return {
setup: setup,
to: scrollToElem,
toY: scrollToY,
intoView: scrollIntoView,
center: scrollToCenterOf,
stop: stopScroll,
moving: function () { return !!scrollTimeoutId },
getY: container.getY,
getTopOf: container.getTopOf
}
}
var docElem = document.documentElement
var getDocY = function () { return window.scrollY || docElem.scrollTop }
// Create a scroller for the document:
var zenscroll = makeScroller({
body: document.scrollingElement || document.body,
toY: function (y) { window.scrollTo(0, y) },
getY: getDocY,
getHeight: function () { return window.innerHeight || docElem.clientHeight },
getTopOf: function (elem) { return elem.getBoundingClientRect().top + getDocY() - docElem.offsetTop }
})
/**
* Creates a scroller from the provided container element (e.g., a DIV)
*
* @param {scrollContainer} The vertical position within the document.
* @param {defaultDuration} Optionally a value for default duration, used for each scroll method by default.
* Ignored if 0 or null or undefined.
* @param {edgeOffset} Optionally a value for the edge offset, used by each scroll method by default.
* Ignored if null or undefined.
* @returns A scroller object, similar to `zenscroll` but controlling the provided element.
*/
zenscroll.createScroller = function (scrollContainer, defaultDuration, edgeOffset) {
return makeScroller({
body: scrollContainer,
toY: function (y) { scrollContainer.scrollTop = y },
getY: function () { return scrollContainer.scrollTop },
getHeight: function () { return Math.min(scrollContainer.clientHeight, window.innerHeight || docElem.clientHeight) },
getTopOf: function (elem) { return elem.offsetTop }
}, defaultDuration, edgeOffset)
}
// Automatic link-smoothing on achors
// Exclude IE8- or when native is enabled or Zenscroll auto- is disabled
if ("addEventListener" in window && !window.noZensmooth && !isNativeSmoothScrollEnabledOn(document.body)) {
var isHistorySupported = "history" in window && "pushState" in history
var isScrollRestorationSupported = isHistorySupported && "scrollRestoration" in history
// On first load & refresh make sure the browser restores the position first
if (isScrollRestorationSupported) {
history.scrollRestoration = "auto"
}
window.addEventListener("load", function () {
if (isScrollRestorationSupported) {
// Set it to manual
setTimeout(function () { history.scrollRestoration = "manual" }, 9)
window.addEventListener("popstate", function (event) {
if (event.state && "zenscrollY" in event.state) {
zenscroll.toY(event.state.zenscrollY)
}
}, false)
}
// Add edge offset on first load if necessary
// This may not work on IE (or older computer?) as it requires more timeout, around 100 ms
if (window.location.hash) {
setTimeout(function () {
// Adjustment is only needed if there is an edge offset:
var edgeOffset = zenscroll.setup().edgeOffset
if (edgeOffset) {
var targetElem = document.getElementById(window.location.href.split("#")[1])
if (targetElem) {
var targetY = Math.max(0, zenscroll.getTopOf(targetElem) - edgeOffset)
var diff = zenscroll.getY() - targetY
// Only do the adjustment if the browser is very close to the element:
if (0 <= diff && diff < 9 ) {
window.scrollTo(0, targetY)
}
}
}
}, 9)
}
}, false)
// Handling clicks on anchors
var RE_noZensmooth = new RegExp("(^|\\s)noZensmooth(\\s|$)")
window.addEventListener("click", function (event) {
var anchor = event.target
while (anchor && anchor.tagName !== "A") {
anchor = anchor.parentNode
}
// Let the browser handle the click if it wasn't with the primary button, or with some modifier keys:
if (!anchor || event.which !== 1 || event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) {
return
}
// Save the current scrolling position so it can be used for scroll restoration:
if (isScrollRestorationSupported) {
var historyState = history.state && typeof history.state === "object" ? history.state : {}
historyState.zenscrollY = zenscroll.getY()
try {
history.replaceState(historyState, "")
} catch (e) {
// Avoid the Chrome Security exception on file protocol, e.g., file://index.html
}
}
// Find the referenced ID:
var href = anchor.getAttribute("href") || ""
if (href.indexOf("#") === 0 && !RE_noZensmooth.test(anchor.className)) {
var targetY = 0
var targetElem = document.getElementById(href.substring(1))
if (href !== "#") {
if (!targetElem) {
// Let the browser handle the click if the target ID is not found.
return
}
targetY = zenscroll.getTopOf(targetElem)
}
event.preventDefault()
// By default trigger the browser's `hashchange` event...
var onDone = function () { window.location = href }
// ...unless there is an edge offset specified
var edgeOffset = zenscroll.setup().edgeOffset
if (edgeOffset) {
targetY = Math.max(0, targetY - edgeOffset)
if (isHistorySupported) {
onDone = function () { history.pushState({}, "", href) }
}
}
zenscroll.toY(targetY, null, onDone)
}
}, false)
}
return zenscroll
}));