简体   繁体   English

滚动无法在带有延迟的SetTimeout()内部工作

[英]Scroll is Not Working Inside SetTimeout() with Delay

I have an scroll to element snippet as: 我有一个滚动到元素片段为:

$('html,body').animate({
     scrollTop: $(window.location.hash).offset().top
},1000);  

which is working perfectly but I need to run it after few seconds on getting into the page (with delay), so I used setTimeout() to get this but now the page jumps to the section (hash) without scrolling or prompting any error message. 它工作正常,但是我需要在进入页面几秒钟后运行(有延迟),所以我使用setTimeout()来获取此信息,但是现在页面跳转到该部分(哈希),而没有滚动或提示任何错误消息。

setTimeout(function(){ 
  $('html,body').animate({
     scrollTop: $(window.location.hash).offset().top
  },1000);   
}, 2000);

can you please let me know what I am doing wrong and why this is not working with delay?! 您能否让我知道我在做错什么,以及为什么这样做没有延误?!

Try with: 尝试:

  $('html,body').delay( 2000 ).animate({
     scrollTop: $(window.location.hash).offset().top
  },1000);

Other option is: 其他选择是:

window.setTimeout(function(){
      $('html,body').animate({
         scrollTop: $(window.location.hash).offset().top
      },1000);
});

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

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