forked from FrontendMatter/material-design-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll-target-behavior.js
More file actions
271 lines (241 loc) · 7.03 KB
/
Copy pathscroll-target-behavior.js
File metadata and controls
271 lines (241 loc) · 7.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
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
import { watch, unwatch } from 'watch-object'
/**
* Allows an element to respond to scroll events from a designated scroll target.
* Use from a consumer via dom-factory mixins i.e.
*
* import { scrollTargetBehavior } from 'material-design-kit'
* const anotherComponent = () => ({
* mixins: [ scrollTargetBehavior ]
* })
*
* @param {HTMLElement} element The element which should respond to scroll events
* @return {Object}
*/
export const scrollTargetBehavior = () => ({
// The scroll target selector
_scrollTargetSelector: null,
// The scroll target HTMLElement
_scrollTarget: null,
/**
* Get the HTMLELement of the scroll target
* @return {HTMLElement}
*/
get scrollTarget () {
if (this._scrollTarget) {
return this._scrollTarget
}
return this._defaultScrollTarget
},
/**
* Set the HTMLElement of the scroll target
* @param {HTMLElement} value
*/
set scrollTarget (value) {
this._scrollTarget = value
},
/**
* Get the scroll target selector
* @return {String|HTMLElement}
*/
get scrollTargetSelector () {
if (this._scrollTargetSelector) {
return this._scrollTargetSelector
}
if (this.element.hasAttribute('scroll-target')) {
return this.element.getAttribute('scroll-target')
}
},
/**
* Set the scroll target selector
* @param {String|HTMLElement} value
*/
set scrollTargetSelector (value) {
this._scrollTargetSelector = value
},
/**
* Get the default scroll target
* @return {HTMLElement}
*/
get _defaultScrollTarget () {
return this._doc
},
/**
* Get the ownerDocument
* @return {HTMLElement}
*/
get _owner () {
return this.element.ownerDocument
},
/**
* Get the document element
* @return {HTMLElement}
*/
get _doc () {
return this._owner.documentElement
},
/**
* Gets the number of pixels that the content of an element is scrolled upward.
* @return {number}
*/
get _scrollTop () {
if (this._isValidScrollTarget()) {
return this.scrollTarget === this._doc ? window.pageYOffset : this.scrollTarget.scrollTop
}
return 0
},
/**
* Sets the number of pixels that the content of an element is scrolled upward.
* @param {number} top
*/
set _scrollTop (top) {
if (this.scrollTarget === this._doc) {
window.scrollTo(window.pageXOffset, top)
}
else if (this._isValidScrollTarget()) {
this.scrollTarget.scrollTop = top
}
},
/**
* Gets the number of pixels that the content of an element is scrolled to the left.
* @return {number}
*/
get _scrollLeft () {
if (this._isValidScrollTarget()) {
return this.scrollTarget === this._doc ? window.pageXOffset : this.scrollTarget.scrollLeft
}
return 0
},
/**
* Sets the number of pixels that the content of an element is scrolled to the left.
* @param {number} left
*/
set _scrollLeft (left) {
if (this.scrollTarget === this._doc) {
window.scrollTo(left, window.pageYOffset)
}
else if (this._isValidScrollTarget()) {
this.scrollTarget.scrollLeft = left
}
},
/**
* Gets the width of the scroll target.
* @return {number}
*/
get _scrollTargetWidth () {
if (this._isValidScrollTarget()) {
return this.scrollTarget === this._doc ? window.innerWidth : this.scrollTarget.offsetWidth
}
return 0
},
/**
* Gets the height of the scroll target.
* @return {number}
*/
get _scrollTargetHeight () {
if (this._isValidScrollTarget()) {
return this.scrollTarget === this._doc ? window.innerHeight : this.scrollTarget.offsetHeight
}
return 0
},
get _isPositionedFixed () {
if (this.element instanceof HTMLElement) {
return window.getComputedStyle(this.element).position === 'fixed'
}
return false
},
/**
* Attach the scroll event listener to the scroll target
* @param {string|HTMLElement} scrollTarget The scroll target (optional)
*/
attachToScrollTarget () {
this.detachFromScrollTarget()
watch(this, 'scrollTargetSelector', this.attachToScrollTarget)
if (this.scrollTargetSelector === 'document') {
this.scrollTarget = this._doc
}
else if (typeof this.scrollTargetSelector === 'string') {
this.scrollTarget = this._owner.querySelector(`#${ this.scrollTargetSelector }`)
}
else if (this.scrollTargetSelector instanceof HTMLElement) {
this.scrollTarget = this.scrollTargetSelector
}
if (this.scrollTarget) {
this.eventTarget = this.scrollTarget === this._doc ? window : this.scrollTarget
this._boundScrollHandler = this._boundScrollHandler || this._scrollHandler.bind(this)
this.eventTarget.addEventListener('scroll', this._boundScrollHandler)
}
},
/**
* Detach the scroll event listener from the scroll target
* @return {[type]} [description]
*/
detachFromScrollTarget () {
unwatch(this, 'scrollTargetSelector', this.attachToScrollTarget)
if (this.eventTarget) {
this.eventTarget.removeEventListener('scroll', this._boundScrollHandler)
}
},
/**
* Scrolls the content to a particular place.
* @param {number} left The left position
* @param {number} top The top position
*/
scroll (left = 0, top = 0) {
if (this.scrollTarget === this._doc) {
window.scrollTo(left, top)
}
else if (this._isValidScrollTarget()) {
this.scrollTarget.scrollLeft = left
this.scrollTarget.scrollTop = top
}
},
/**
* Scrolls the content to a particular place using a behavior.
* @param {Number} left The left position
* @param {Number} top The top position
* @param {String} behavior The behavior name
* @param {Function} scrollFn Custom scroll timing function used with `behavior` (optional)
*/
scrollWithBehavior (left = 0, top = 0, behavior, scrollFn) {
// Scroll timing function used with `behavior`
scrollFn = typeof scrollFn === 'function' ? scrollFn : function easeOutQuad (t, b, c, d) {
t /= d
return -c * t * (t - 2) + b
}
// Smooth
if (behavior === 'smooth') {
let startTime = Date.now()
let currentScrollTop = this._scrollTop
let currentScrollLeft = this._scrollLeft
let dScrollTop = top - currentScrollTop
let dScrollLeft = left - currentScrollLeft
let duration = 300;
(function updateFrame () {
let now = Date.now()
let elapsedTime = now - startTime
if (elapsedTime < duration) {
this.scroll(
scrollFn(elapsedTime, currentScrollLeft, dScrollLeft, duration),
scrollFn(elapsedTime, currentScrollTop, dScrollTop, duration)
)
requestAnimationFrame(updateFrame.bind(this))
}
}).call(this)
}
// Default
else {
this.scroll(left, top)
}
},
/**
* Returns true if the scroll target is a valid HTMLElement.
* @return {Boolean}
*/
_isValidScrollTarget () {
return this.scrollTarget instanceof HTMLElement
},
/**
* Scroll event handler (runs on every scroll event)
*/
_scrollHandler () {}
})