Animate scroll to ID on page load

前端 未结 6 2188
野性不改
野性不改 2020-11-28 01:39

Im tring to animate the scroll to a particular ID on page load. I have done lots of research and came across this:

$(\"html, body\").animate({ scrollTop: $(\         


        
6条回答
  •  攒了一身酷
    2020-11-28 01:45

    You are only scrolling the height of your element. offset() returns the coordinates of an element relative to the document, and top param will give you the element's distance in pixels along the y-axis:

    $("html, body").animate({ scrollTop: $('#title1').offset().top }, 1000);
    

    And you can also add a delay to it:

    $("html, body").delay(2000).animate({scrollTop: $('#title1').offset().top }, 2000);
    

提交回复
热议问题