Skip to content

Instantly share code, notes, and snippets.

@rasadeghnasab
Last active October 23, 2021 22:38
Show Gist options
  • Save rasadeghnasab/3671c07898e509eeacf7 to your computer and use it in GitHub Desktop.
Save rasadeghnasab/3671c07898e509eeacf7 to your computer and use it in GitHub Desktop.
parallax_js
$(document).ready(function(){
$(window).on('scroll',function(e){
parallax();
});
$('a.earth,a.moon,a.saturn,a.rocket').on('click', function(e){
e.preventDefault();
var th = $(this),
target = $(this).attr('href'),
next_pos = $(target).offset().top,
current_pos = $(window).scrollTop(),
difference = Math.floor(current_pos) - Math.floor(next_pos);
/* if we click on the link that we're on do not perform any action */
if(difference > -5 && difference < 5 ) return;
/* a little motion before move to other section */
current_pos = next_pos > current_pos ? current_pos - 60 : current_pos + 60;
$('html, body').animate({scrollTop: current_pos}, 150,
function(){
parallax();
}).animate({scrollTop: next_pos}, 1000,
function(){
parallax();
})
})
});
function parallax(){
var scrollPosition = $(window).scrollTop() * -1;
$('#stars').css('top',scrollPosition * 0.2 + 'px');
$('#images').css('top',scrollPosition * 0.5 + 'px');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment