-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallax.js
55 lines (51 loc) · 1.41 KB
/
parallax.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
document.addEventListener('DOMContentLoaded', function(){
var Parallax = function (qry, options) {
this.options = Object.assign({
multiplier: 1,
fadeToBlack: false
}, options);
this.query = qry;
this.listeners = [];
this.elems = document.querySelectorAll(this.query);
this.init();
}
Parallax.prototype = {
init: function(){
for (var i = 0; i < this.elems.length; i++) {
this.listeners[i] = function(self, index){
self.setBackgroundPosition(self.elems[index]);
}
}
this.initListeners();
this.loadParallax();
},
initListeners: function(){
var _self = this;
window.addEventListener('scroll', function(){
_self.runParallax();
});
},
runParallax: function(){
for (var i = 0; i < this.listeners.length; i++) {
this.listeners[i](this, i);
}
},
setBackgroundPosition: function(elem){
if (window.scrollY + elem.offsetTop < elem.offsetTop || elem.offsetTop + elem.offsetHeight < window.scrollY) return false;
var backpositiony = this.calcPosition(elem);
elem.style.backgroundPosition = '50% '+(((backpositiony * this.options.multiplier)-100)*-1)+'%';
},
calcPosition: function(elem){
var min = elem.offsetTop;
var max = elem.offsetTop + elem.offsetHeight;
var range = max - min;
return window.scrollY / range * 100;
},
loadParallax: function(){
this.runParallax();
}
}
var plx = new Parallax('.parallax', {
multiplier: 4
});
});