forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathparallax.ts
151 lines (132 loc) · 4.72 KB
/
parallax.ts
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
import { Utils } from './utils';
import { Component, BaseOptions, InitElements, MElement } from './component';
export interface ParallaxOptions extends BaseOptions {
/**
* The minimum width of the screen, in pixels, where the parallax functionality starts working.
* @default 0
*/
responsiveThreshold: number;
}
const _defaults: ParallaxOptions = {
responsiveThreshold: 0 // breakpoint for swipeable
};
export class Parallax extends Component<ParallaxOptions> {
private _enabled: boolean;
private _img: HTMLImageElement;
static _parallaxes: Parallax[] = [];
static _handleScrollThrottled: () => Utils;
static _handleWindowResizeThrottled: () => Utils;
constructor(el: HTMLElement, options: Partial<ParallaxOptions>) {
super(el, options, Parallax);
this.el['M_Parallax'] = this;
this.options = {
...Parallax.defaults,
...options
};
this._enabled = window.innerWidth > this.options.responsiveThreshold;
this._img = this.el.querySelector('img');
this._updateParallax();
this._setupEventHandlers();
this._setupStyles();
Parallax._parallaxes.push(this);
}
static get defaults(): ParallaxOptions {
return _defaults;
}
/**
* Initializes instance of Parallax.
* @param el HTML element.
* @param options Component options.
*/
static init(el: HTMLElement, options?: Partial<ParallaxOptions>): Parallax;
/**
* Initializes instances of Parallax.
* @param els HTML elements.
* @param options Component options.
*/
static init(els: InitElements<MElement>, options?: Partial<ParallaxOptions>): Parallax[];
/**
* Initializes instances of Parallax.
* @param els HTML elements.
* @param options Component options.
*/
static init(
els: HTMLElement | InitElements<MElement>,
options: Partial<ParallaxOptions> = {}
): Parallax | Parallax[] {
return super.init(els, options, Parallax);
}
static getInstance(el: HTMLElement): Parallax {
return el['M_Parallax'];
}
destroy() {
Parallax._parallaxes.splice(Parallax._parallaxes.indexOf(this), 1);
this._img.style.transform = '';
this._removeEventHandlers();
this.el['M_Parallax'] = undefined;
}
static _handleScroll() {
for (let i = 0; i < Parallax._parallaxes.length; i++) {
const parallaxInstance = Parallax._parallaxes[i];
parallaxInstance._updateParallax.call(parallaxInstance);
}
}
static _handleWindowResize() {
for (let i = 0; i < Parallax._parallaxes.length; i++) {
const parallaxInstance = Parallax._parallaxes[i];
parallaxInstance._enabled = window.innerWidth > parallaxInstance.options.responsiveThreshold;
}
}
_setupEventHandlers() {
this._img.addEventListener('load', this._handleImageLoad);
if (Parallax._parallaxes.length === 0) {
if (!Parallax._handleScrollThrottled) {
Parallax._handleScrollThrottled = Utils.throttle(Parallax._handleScroll, 5);
}
if (!Parallax._handleWindowResizeThrottled) {
Parallax._handleWindowResizeThrottled = Utils.throttle(Parallax._handleWindowResize, 5);
}
window.addEventListener('scroll', Parallax._handleScrollThrottled);
window.addEventListener('resize', Parallax._handleWindowResizeThrottled);
}
}
_removeEventHandlers() {
this._img.removeEventListener('load', this._handleImageLoad);
if (Parallax._parallaxes.length === 0) {
window.removeEventListener('scroll', Parallax._handleScrollThrottled);
window.removeEventListener('resize', Parallax._handleWindowResizeThrottled);
}
}
_setupStyles() {
this._img.style.opacity = '1';
}
_handleImageLoad = () => {
this._updateParallax();
};
private _offset(el: Element) {
const box = el.getBoundingClientRect();
const docElem = document.documentElement;
return {
top: box.top + window.scrollY - docElem.clientTop,
left: box.left + window.scrollX - docElem.clientLeft
};
}
_updateParallax() {
const containerHeight =
this.el.getBoundingClientRect().height > 0 ? this.el.parentElement.offsetHeight : 500;
const imgHeight = this._img.offsetHeight;
const parallaxDist = imgHeight - containerHeight;
const bottom = this._offset(this.el).top + containerHeight;
const top = this._offset(this.el).top;
const scrollTop = Utils.getDocumentScrollTop();
const windowHeight = window.innerHeight;
const windowBottom = scrollTop + windowHeight;
const percentScrolled = (windowBottom - top) / (containerHeight + windowHeight);
const parallax = parallaxDist * percentScrolled;
if (!this._enabled) {
this._img.style.transform = '';
} else if (bottom > scrollTop && top < scrollTop + windowHeight) {
this._img.style.transform = `translate3D(-50%, ${parallax}px, 0)`;
}
}
}