Skip to content

Instantly share code, notes, and snippets.

@emilaspman
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilaspman/d393933549841e4b0b08 to your computer and use it in GitHub Desktop.
Save emilaspman/d393933549841e4b0b08 to your computer and use it in GitHub Desktop.
simple background-image parallax
$(function() {
var offset = [], height = [], windowHeight = 0;
var $window = $(window);
var $parallax = $('.parallax');
$window.resize(measure).bind('scroll', update);
measure();
function measure() {
$parallax.each(function(i) {
$parent = $(this).parent('div');
offset[i] = $parent.offset().top;
height[i] = $parent.height();
});
windowHeight = $window.height();
update();
}
function update() {
var pos = $window.scrollTop();
$parallax.each(function(i) {
if(pos + windowHeight > offset[i] && pos - height[i] < offset[i]) {
var translate = 'translate3d(0, '+ Math.round((pos - offset[i]) * 0.7) + 'px, 0)';
$(this).css({'-webkit-transform':translate,'transform':translate});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment