-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallax-bg.js
198 lines (177 loc) · 6.44 KB
/
parallax-bg.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
// todo resizeObserver?
const pool = new Set();
const paraxBg = {
add(element){
pool.add(element)
pool.size === 1 && addListeners();
//element.connect();
},
remove(element){
pool.delete(element);
//pool.size === 0 && removeListeners(); // todo
},
positionize(){
//requestAnimationFrame(()=>{
pool.forEach(item=>item.positionize());
//});
},
layout(){
pool.forEach(item=>item.layout());
}
}
// cache dimensions. Is it worth it?
let pageY;
let winHeight;
let scrollHeight;
function setVPDimensions(){
pageY = scrollY;
winHeight = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0); // was innerHeight, better this?: https://stackoverflow.com/questions/1248081/how-to-get-the-browser-viewport-dimensions
scrollHeight = document.documentElement.scrollHeight;
}
setVPDimensions();
function addListeners(){
addEventListener('DOMContentLoaded', paraxBg.layout);
addEventListener('load', ()=>{
paraxBg.layout();
paraxBg.positionize();
});
document.addEventListener('scroll', ()=>{
pageY = scrollY;
requestAnimationFrame(()=>paraxBg.positionize()) // better!
});
addEventListener('wheel', ()=>{ // for firefox
pageY = scrollY;
requestAnimationFrame(()=>paraxBg.positionize())
});
addEventListener('resize', ()=>{
setVPDimensions();
paraxBg.layout();
paraxBg.positionize();
});
const rs = new ResizeObserver(entries => {
setVPDimensions();
paraxBg.layout();
paraxBg.positionize();
})
rs.observe(document.body);
}
const style = document.createElement('style');
style.innerHTML =
'.u1-parallax-bg-stage { position:relative; } '+
'parallax-bg { position:absolute; top:0; bottom:0; left:0; right:0; z-index:-1; will-change:transform; background-size:cover; } '+
document.head.prepend(style);
class ParallaxBg extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({mode:'open'});
shadowRoot.innerHTML = `
<style>
:host {
position:absolute;
overflow:hidden;
top:0; left:0; right:0; // todo if safari: inset
bottom:-.2px;
xz-index:-1;
xwill-change:transform;
xbackground-size:cover;
}
.mover {
position:absolute;
top:0; bottom:0; left:0; right:0;
z-index:-1;
will-change:transform;
background: inherit;
}
.visible {
display:flex;
flex-direction:row;
align-items:stretch;
justify-content:center;
position:absolute;
top:0; bottom:0; left:0; right:0;
}
</style>
<div class=mover part=_mover>
<slot class=visible></slot>
</div>
`;
// .mover {will-change:transform} brings a lot, but the budget is easily exceeded... we should only do this if in the visible area.
this.mover = this.shadowRoot.querySelector('.mover');
this.visible = this.shadowRoot.querySelector('.visible');
const style = getComputedStyle(this);
const speed = style.getPropertyValue('--parallax-bg-speed');
this.speed = speed === '' ? .5 : parseFloat(speed);
}
connectedCallback() {
/*
let stage = this.closest('.u1-parallax-bg-stage');
if (!stage) {
stage = this.parentNode;
stage.setAttribute('parallax-bg-stage','');
}
*/
this.stage = this.offsetParent;
if (this.stage.tagName === 'BODY') this.stage.style.position = 'relative'; // what can go wrong?
scrollHeight = document.documentElement.scrollHeight; // todo: little slow
this.layout();
this.positionize();
paraxBg.add(this);
}
disconnectedCallback(){
paraxBg.remove(this);
}
layout(){
const rect = this.stage.getBoundingClientRect();
this.stageRect = { // todo: add border-width
top: pageY + rect.top,
bottom: pageY + rect.bottom,
height: rect.height,
yCenter: (rect.top + pageY) + rect.height/2,
};
// calculate offset if stage is on top
let relevantTop = this.stageRect.top;
// if its faster then normal, calculate offset on bottom of stage
if (this.speed > 1) relevantTop += this.stageRect.height;
let offset = this.offsetAtPageY(relevantTop);
offset = Math.abs(offset);
// if it moves the opposite, add the stage height to the offset
if (this.speed < 0) offset += (-this.speed * this.stageRect.height);
this.mover.style.top = -offset + 'px';
this.mover.style.bottom = -offset + 'px';
// if the element cannot go further down or up
// the [parallax-bg-visible] element
// the most complicated part of the lib, seems to work well, but it was born by trial and error
if (this.speed < 0) {
console.warn('parallax-bg: parallax-bg-visible attribute is not implemented for speed < 0');
return;
}
//const visibleEl = this.querySelector('[parallax-bg-visible]');
const visibleEl = this.visible;
if (visibleEl) {
let top = 0;
let bottom = 0;
const offsetAtElTop = this.offsetAtPageY(this.stageRect.top);
const offsetAtTop = this.offsetAtPageY(0);
const offsetAtElBottom = this.offsetAtPageY(this.stageRect.bottom - winHeight);
const offsetAtBottom = this.offsetAtPageY(scrollHeight - winHeight);
if (this.speed < 1) {
top = Math.min(offsetAtElTop, offsetAtBottom);
bottom = Math.max(offsetAtTop, offsetAtElBottom);
}
if (this.speed > 1) {
top = Math.max(offsetAtTop, -offsetAtElBottom);
bottom = Math.min(-offsetAtElTop, offsetAtBottom);
}
visibleEl.style.top = offset - top + 'px';
visibleEl.style.bottom = offset + bottom + 'px';
}
}
offsetAtPageY(pageY){
const moved = this.stageRect.yCenter - (pageY + winHeight/2);
return moved*(this.speed-1);
}
positionize(){
this.mover.style.transform = 'translate3d(0, '+ this.offsetAtPageY(pageY) +'px, 0)';
}
}
customElements.define('u1-parallax-bg', ParallaxBg)