简体   繁体   English

几秒钟后滚动到一个 div

[英]Scroll to a div after a few seconds

I am trying to smooth scroll to a div after about a minute on a page.我试图在页面上大约一分钟后平滑滚动到 div。 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.我更喜欢使用 jQuery 但我也对 JavaScript 持开放态度。

Here is what I have so far:这是我到目前为止所拥有的:

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

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);

You can use Something like this which is quite easy.你可以使用这样的东西,这很容易。 Just Create a function with some name and call it after few seconds.只需创建一个带有一些名称的 function 并在几秒钟后调用它。

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

    }


    window.setTimeout( scrolltodiv, 5000 );
});
$(function(){
    setTimeout(function(){
      animate("#idorclass" ,2000)
    }, 5000)
})

const animate = (idorclass, animval)=>{
  $('html, body').animate({
    scrollTop: $(idorclass).offset().top
 }, animval);
}

also dynamic function that you can reuse还可以重复使用的动态 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM