Skip to content

Instantly share code, notes, and snippets.

@goldhand
Created January 30, 2015 01:07
Show Gist options
  • Save goldhand/9b9faa2dea84f66db93a to your computer and use it in GitHub Desktop.
Save goldhand/9b9faa2dea84f66db93a to your computer and use it in GitHub Desktop.
simple javascript parallax script
// 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