Skip to content

Instantly share code, notes, and snippets.

@Alexander-Pop
Created November 19, 2021 04:39
Show Gist options
  • Save Alexander-Pop/0f281688be2a94a8901c45af0f4c34a0 to your computer and use it in GitHub Desktop.
Save Alexander-Pop/0f281688be2a94a8901c45af0f4c34a0 to your computer and use it in GitHub Desktop.
Play with Parallax #parallax #js $jq #animation
const main = {};
const updateScroll = () => {
main.winScroll = $(window).scrollTop();
};
const parallaxLoad = () => {
const $someEl = $('.someEl');
$someEl.each((i,el) => {
let $el = $(el);
main.someEl[i] = $el;
});
}
const parallaxScroll = () => {
$.each(main.someEl, (i,$el) => {
$el.imgPos = (main.winScroll - $el.offset().top) / 20;
$el.css('transform', 'translateY(' + $el.imgPos + '%)');
});
}
// Compile
const load = () => {
parallaxLoad();
}
const scroll = () => {
updateScroll();
parallaxScroll();
}
// Events
$(window).scroll(() => {
scroll();
});
load();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment