Skip to content Skip to sidebar Skip to footer

Jquery Scrolltop To Multiples Id + Settimeout

This is my case, i have a page with mutiples div/id created from a php while loop, based on the amount of pdf files that exists on a folder. My goal is to scroll to each one of the

Solution 1:

Try this code adjust $pdf_time & totalPDF as per your need:

<script>
  var totalPDF = 7;
  var currentPDF = 0;
  var loopInterval = <?php echo $pdf_time; ?>;
  $(function(){
    function loopanchors(){
      if(totalPDF <= currentPDF++){
        currentPDF = 1;
      }

      /* scroll to currentPDF */
      setTimeout(function(){
        console.log('scrolling to', currentPDF);
        try{
          $('html, body').animate({
            scrollTop: $('#' + currentPDF).offset().top
          },
          2000);
        }catch(e){
          console.log('element not found');
        }

        /* scroll next pdf */
        loopanchors();
      }, (loopInterval * currentPDF));
    }

    /*start it up the first time*/
    loopanchors();
  });
</script>

Post a Comment for "Jquery Scrolltop To Multiples Id + Settimeout"