Raphael Bergeron Raphael Bergeron - 6 months ago 14
jQuery Question

scroll down then up 2 time when page is loaded

hi guys i'm looking for a jquery that would make my page scroll to the bottom of the page the to the top 2 time, so ( down,up,down,up) Thanks !:) I have this already that works but it only do it one time and i dont know how to make it do it twice!

<script>
$(document).ready(function() {
$('html, body').animate({
scrollTop: $(document).height() - $(window).height() },
8000, function() {
$(this).animate({ scrollTop: 0 }, 4000);

});
});
</script>

Answer

here you go: DEMO

  var scrollCount=0;
  var scroll=function(direction){
    if(scrollCount<4){
      var scrollTo= (direction==='down') ? $(document).height() - $(window).height() : 0;
      $('html, body').animate({ 
        scrollTop: scrollTo },
        800);
      setTimeout(function(){
        scrollCount++;
        direction=(direction==='down') ? 'up' : 'down';
        scroll(direction); 
      },850);
    }
  }
  scroll('down');

for some reason the callback on the animation didn't work: Failed Demo