Skip to content

Instantly share code, notes, and snippets.

@khripunovpp
Last active October 2, 2018 09:07
Show Gist options
  • Save khripunovpp/56d526631489a53458ff0ee8846397b7 to your computer and use it in GitHub Desktop.
Save khripunovpp/56d526631489a53458ff0ee8846397b7 to your computer and use it in GitHub Desktop.
parallax
var parallax = function() {
$(window).on('scroll', function() {
var st = $(window).scrollTop();
$('.js-parallax-container').each(function() {
var containerOffset = $(this).offset(),
containerOffsetTop = containerOffset.top,
containerHeight = $(this).outerHeight();
scrollTop = (st - containerOffsetTop) + (containerHeight/4); // ставим точку отсчета паралакса от середины контейнера
$('.js-parallax', this).each(function() {
var speed = 10,
reverse = $(this).data('reverse'),
shift = scrollTop / speed;
if ($(this).data('reverse') === true) shift = -shift;
$(this).css({
"transform": "translate(0%, " + shift + "%"
})
});
})
});
}
$(function() {
parallax()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment