Created
January 30, 2015 01:07
-
-
Save goldhand/9b9faa2dea84f66db93a to your computer and use it in GitHub Desktop.
simple javascript parallax script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create cross browser requestAnimationFrame method: | |
window.requestAnimationFrame = window.requestAnimationFrame | |
|| window.mozRequestAnimationFrame | |
|| window.webkitRequestAnimationFrame | |
|| window.msRequestAnimationFrame | |
|| function(f){setTimeout(f, 1000/60)} | |
var parallaxElem = document.getElementById('parallax-content'); | |
var scrollRate = .5; // move parallaxElem at 50% of scroll rate | |
function parallaxThis() { | |
var scrolltop = window.pageYOffset; | |
parallaxElem.style.marginTop = (-scrolltop * scrollRate) + 'px'; | |
}; | |
window.addEventListener('scroll', function() { | |
requestAnimationFrame(parallaxThis); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment