Home > Enterprise >  Scroll to a div after a few seconds
Scroll to a div after a few seconds

Time:01-20

I am trying to scroll to a div after about a minute on a page. I looked on here and found this answer but it did not help me as the person who gave the answer didn't really answer the person's question.

I'd prefer to use jQuery but I am open to JavaScript as well.

Here is what I have so far:

$(document).ready(function() {
        $('body').delay(5000) 
        .animate({
      'scrollTop': $('#usp').offset().top
        }, 5000); 
    });

CodePudding user response:

I hope this helps:

( function($){

setTimeout( function() {
$('html, body').animate({
        scrollTop: $("#elementID").offset().top
        // you can use $(".elementClass") but as ID should be unique, it would be better to use an element ID instead of classes
    }, 2000);
    // 2000 ms is the animation duration

}, 5000) 
// it scrolls to #elementID after 5000 ms = 5 secs

} )(jQuery);

CodePudding user response:

You can use Something like this which is quite easy. Just Create a function with some name and call it after few seconds.

$(document).ready(function() {
    function scrolltodiv(){
            $('html, body').animate({
                scrollTop: $("#myDiv").offset().top
            }, 2000);

    }


    window.setTimeout( scrolltodiv, 5000 );
});
  •  Tags:  
  • Related