Last active
November 8, 2016 09:13
-
-
Save alizhdanov/a3d716700c7d1d30cee0d303f134504f to your computer and use it in GitHub Desktop.
Moving body background
This file contains hidden or 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
// 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