Last active
August 29, 2015 13:56
-
-
Save emilaspman/d393933549841e4b0b08 to your computer and use it in GitHub Desktop.
simple background-image parallax
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
$(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