Skip to content

Instantly share code, notes, and snippets.

@celticwebdesign
Created May 27, 2019 09:28
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 celticwebdesign/f9edd7983c346ec97fd5704af30b1364 to your computer and use it in GitHub Desktop.
Save celticwebdesign/f9edd7983c346ec97fd5704af30b1364 to your computer and use it in GitHub Desktop.
Any '#full_width_single_image' scroll snap.
// Start - any full_width_single_image scroll snap
// https://jsfiddle.net/vqakpboe/1/
if ( $("#full_width_single_image").length > 0 ) {
var timeout = 400;
var timer;
var top = true;
$(window).scroll(function(event){
clearTimeout(timer);
// stop any existing timer
// if multiple scrolls are detected then always cancel timer and next line start a new one and hence pause for 'timeout' value before the auto scroll snap occurs.
// set a new timer
timer = setTimeout(function() {
// Timer, means the scroll snap only works after the user has stopped scrolling, this prevents the scroll happening continually and during the users scroll.
var scrollTop = $(window).scrollTop();
if (scrollTop == 0) {
top = true;
// Only allows scroll snap down if the user is at the top or returns to the top (and then scrolls down)
}
var scroll_div_height = $('#full_width_single_image').height();
// the div I want to scroll past.
if(
scroll_div_height > scrollTop &&
top == true &&
scrollTop > 0
) {
$('html,body').animate({scrollTop: scroll_div_height},'slow');
top = false;
return;
}
}, timeout);
});
}
// Finish - any full_width_single_image scroll snap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment