forked from FrontendMatter/material-design-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfade-background.js
More file actions
51 lines (45 loc) · 1.52 KB
/
Copy pathfade-background.js
File metadata and controls
51 lines (45 loc) · 1.52 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
const FRONT_LAYER = '[class*="__bg-front"]'
const REAR_LAYER = '[class*="__bg-rear"]'
/**
* fade-background effect
*/
export const SCROLL_EFFECT_FADE_BACKGROUND = {
name: 'fade-background',
setUp (config) {
const duration = config.duration || '0.5s'
const threshold = config.threshold || (this._isPositionedFixed ? 1 : 0.3)
let frontLayer = this.element.querySelector(FRONT_LAYER)
let rearLayer = this.element.querySelector(REAR_LAYER)
const layers = [ frontLayer, rearLayer ]
layers.map(layer => {
if (layer) {
let willChange = layer.style.willChange.split(',').map(c => c.trim()).filter(c => c.length)
willChange.push('opacity', 'transform')
layer.style.willChange = [...new Set(willChange)].join(', ')
if (layer.style.transform === '') {
this._transform('translateZ(0)', layer)
}
layer.style.transitionProperty = 'opacity'
layer.style.transitionDuration = duration
}
})
this._fadeBackgroundThreshold = !this._isPositionedFixed
? threshold + (this._progress * threshold)
: threshold
},
tearDown () {
delete this._fadeBackgroundThreshold
},
run (progress, top) {
let frontLayer = this.element.querySelector(FRONT_LAYER)
let rearLayer = this.element.querySelector(REAR_LAYER)
if (progress >= this._fadeBackgroundThreshold) {
frontLayer.style.opacity = 0
rearLayer.style.opacity = 1
}
else {
frontLayer.style.opacity = 1
rearLayer.style.opacity = 0
}
}
}