Skip to content

Instantly share code, notes, and snippets.

@tpitre
Last active June 1, 2016 21:23
Show Gist options
  • Save tpitre/0d829f233c2e58e63b70899c98b64a6e to your computer and use it in GitHub Desktop.
Save tpitre/0d829f233c2e58e63b70899c98b64a6e to your computer and use it in GitHub Desktop.
Simple Parallax Effect
// Is it a touch device? (requires Modernizr)
var isTouch = (Modernizr.touch) ? true : false;
// Background image parallax.
var parallax = function($b) {
if (!isTouch) {
var scrolled = $(window).scrollTop();
$b.css('background-position', 'center ' + -(scrolled * 0.5) + 'px');
}
}
$(window).scroll(function() {
// Init parallax on element with background image when scrolling.
parallax($('.hero__image'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment