Skip to content

Instantly share code, notes, and snippets.

@iamklim
Last active January 30, 2019 13:46
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 iamklim/32125821b02e3254319a0380cb1c01a7 to your computer and use it in GitHub Desktop.
Save iamklim/32125821b02e3254319a0380cb1c01a7 to your computer and use it in GitHub Desktop.
function axelerateBackground(bgWrapperHeight) {
var $bgWrapper = $('.bg_wrapper'); //parallax block inside body with position fixed and css background
$(document).on('scroll', function() {
var dHeight = $(document).height(),
wHeight = $(window).height(),
imgHeight = bgWrapperHeight ? bgWrapperHeight : $bgWrapper.height(), //height of parallax block
coeff = (imgHeight - wHeight) / (dHeight - wHeight); //coefficient to avoid empty spaces on background
var shift = -1 * ($(window).scrollTop() * coeff); //size of shift on the top
if (dHeight - wHeight > 20) $bgWrapper.css('top', shift + "px");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment