Skip to content

Instantly share code, notes, and snippets.

@alizhdanov
Last active November 8, 2016 09:13
Show Gist options
  • Save alizhdanov/a3d716700c7d1d30cee0d303f134504f to your computer and use it in GitHub Desktop.
Save alizhdanov/a3d716700c7d1d30cee0d303f134504f to your computer and use it in GitHub Desktop.
Moving body background
// single-line solution
$('body').css('background-position', 'center ' + (-window.pageYOffset / 8) + 'px');
// more advanced solution
$('.paralax').each(function() {
var position = (-window.pageYOffset) + $(this).offset().top
position = -(position / 8)
if (position >= ($(this).height() + $(this).height() / 2)) {
position = 'bottom'
}
else if (position < -($(this).height() / 2)) {
position = 'top'
}
else {
position += 'px'
}
console.log(position)
$(this).css('background-position', 'center ' + position)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment