-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparallaxPosition.js
More file actions
45 lines (37 loc) · 1.2 KB
/
Copy pathparallaxPosition.js
File metadata and controls
45 lines (37 loc) · 1.2 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
var transform = ['transform', 'msTransform', 'webkitTransform', 'mozTransform', 'oTransform'];
var parallaxElem = document.querySelectorAll('[data-parallax]');
var getSupportedPropertyName = function(properties) {
for (var i = 0; i < properties.length; i++) {
if (typeof document.body.style[properties[i]] !== 'undefined') {
return properties[i];
}
}
return null;
};
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ) {
window.setTimeout(callback, 1000 / 60);
};
})();
var parallax = function(elem, rate) {
elem.style[getSupportedPropertyName(transform)] = 'translate3d(0px,' + Math.round(wScrollCurrent * rate) + 'px, 0px)';
};
var parallaxScroll = function() {
forEach( parallaxElem, function ( value ) {
var speed = value.getAttribute('data-parallax');
parallax(value, speed);
});
};
var scrollEventHandler = function() {
if (parallaxElem) {
requestAnimFrame( parallaxScroll );
}
};
window.addEventListener( 'scroll', scrollEventHandler, false );
// Usage
<div class="elem" data-parallax=".5"></div>