i76 i76 - 2 months ago 23
HTML Question

jQuery-- scroll to id after x(pixel) scroll

Is it possible to scroll to an #id after a number of pixels have been scrolled (and the div below does not scroll up) by modifying the code below?

$(".bhome").click(function() {
$('html, body').animate({
scrollTop: $("#someid").offset().top
}, 2000);


});

Answer

This is not the last Solution, but this could help you make the first steps:

$(document).ready(function(){
  var numberOfPixels = 20;
  var lastScrollTop = 0;
  $(window).scroll(function(event){
    var st = $(this).scrollTop();
    if (st > lastScrollTop){
       // downscroll code
       if($(window).scrollTop() > numberOfPixels) {
         $('html, body').animate({
           scrollTop: $("#someid").offset().top
         }, 2000);
         //$(window).off('scroll');
       }
    } else {
      // upscroll code
    }
   lastScrollTop = st;
  });
});