forked from FrontendMatter/material-design-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox.js
More file actions
167 lines (140 loc) · 3.75 KB
/
Copy pathbox.js
File metadata and controls
167 lines (140 loc) · 3.75 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
import { scrollTargetBehavior } from '../scroll-target-behavior'
import { scrollEffectBehavior } from '../scroll-effect-behavior'
import { handler } from 'dom-factory'
// SCROLL EFFECTS
import { SCROLL_EFFECTS } from '../scroll-effects'
const MODULE = 'mdk-box'
const BG = `.${ MODULE }__bg`
const FRONT_LAYER = `${ BG }-front`
const REAR_LAYER = `${ BG }-rear`
/**
* A container element for generic content with visual effects based on scroll position
* @param {HTMLElement} element
* @return {Object}
*/
export const boxComponent = (element) => ({
/**
* Public properties.
* @type {Object}
*/
properties: {
/**
* Disables effects
*/
disabled: {
type: Boolean,
reflectToAttribute: true
}
},
/**
* Event listeners.
* @type {Array}
*/
listeners: [
'window._debounceResize(resize)'
],
/**
* Compose mixins
* @type {Array}
*/
mixins: [
scrollTargetBehavior(element),
scrollEffectBehavior(element)
],
// The current scroll progress
_progress: 0,
/**
* Returns true if the element is visible in the current viewport.
* @return {Boolean}
*/
isOnScreen () {
return this._elementTop < this._scrollTop + this._scrollTargetHeight &&
this._elementTop + this._elementHeight > this._scrollTop
},
/**
* Returns an object containing the progress value of the scroll effects.
* @return {Object}
*/
getScrollState () {
return {
progress: this._progress
}
},
_setupBackgrounds () {
let bgNode = document.createElement('DIV')
this.element.insertBefore(bgNode, this.element.childNodes[0])
bgNode.classList.add(BG.substr(1))
const bgLayerClassNames = [FRONT_LAYER.substr(1), REAR_LAYER.substr(1)]
bgLayerClassNames.map(className => {
let bgNodeLayer = document.createElement('DIV')
bgNode.appendChild(bgNodeLayer)
bgNodeLayer.classList.add(className)
})
},
_resetLayout () {
if (this.element.offsetWidth === 0 && this.element.offsetHeight === 0) {
return
}
let scrollTop = this._clampedScrollTop
this._elementTop = this._getElementTop()
this._elementHeight = this.element.offsetHeight
let viewportTop = this._elementTop - scrollTop
this._progressTarget = Math.min(this._scrollTargetHeight, viewportTop + this._elementHeight)
this._setUpEffects()
this._updateScrollState(scrollTop)
},
_getElementTop () {
let currentNode = this.element
let top = 0
while (currentNode && currentNode !== this.scrollTarget) {
top += currentNode.offsetTop
currentNode = currentNode.offsetParent
}
return top
},
/**
* Updates the scroll state.
* @param {number} scrollTop
*/
_updateScrollState (scrollTop) {
if (this.disabled) {
return
}
if (this.isOnScreen()) {
let viewportTop = this._elementTop - scrollTop
let progress = 1 - (viewportTop + this._elementHeight) / this._progressTarget
this._progress = progress
this._runEffects(this._progress, scrollTop)
}
},
/**
* Handle the resize event every 50ms
*/
_debounceResize () {
clearTimeout(this._onResizeTimeout)
if (this._resizeWidth !== window.innerWidth) {
this._onResizeTimeout = setTimeout(() => {
this._resizeWidth = window.innerWidth
this._resetLayout()
}, 50)
}
},
/**
* Initialize component
*/
init () {
this._resizeWidth = window.innerWidth
this.attachToScrollTarget()
this._setupBackgrounds()
this._resetLayout()
SCROLL_EFFECTS.map(effect => this.registerEffect(effect.name, effect))
},
/**
* Destroy component
*/
destroy () {
clearTimeout(this._onResizeTimeout)
this.detachFromScrollTarget()
}
})
handler.register(MODULE, boxComponent)