Skip to content

Instantly share code, notes, and snippets.

@thotbox
Created August 14, 2014 13:03
Show Gist options
  • Select an option

  • Save thotbox/d1b44298c14067f57f6c to your computer and use it in GitHub Desktop.

Select an option

Save thotbox/d1b44298c14067f57f6c to your computer and use it in GitHub Desktop.
JavaScript: Responsive Parallax
// Parallax
$(document).ready(function(){
$window = $(window);
var width = $window.width();
$(window).bind('resize', function () {
width = $window.width();
});
$(window).scroll(function() {
// #hero
var offset = 0;
var speed;
if (width > 640) {
speed = 5;
}
if (width > 1024) {
speed = 5;
}
if (width > 1300) {
speed = -5;
}
var calc = ($window.scrollTop() / speed) + offset;
var results = '50% '+ calc + 'px';
if (width > 640) {
$('#hero').css({ backgroundPosition: results });
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment