几秒钟后检查文档的scrollTop()

时间:2014-12-24 13:34:44

标签: javascript jquery scroll delay scrolltop

我想在页面加载后两秒检查$(document).scrollTop()。我怎样才能做到这一点?有人能帮助我吗?我是jQuery的新手吗?

我试过这个,但它不起作用:

$(document).delay(2000).scrollTop()

2 个答案:

答案 0 :(得分:9)

您可以在document.ready被触发后使用setTimeout()功能执行此操作:

$(function(){ //wait for document ready

  setTimeout(function(){
    $(document).scrollTop() 
  }, 2000) //execute your function after 2 seconds.

});

答案 1 :(得分:0)

第一步是在加载文档时触发事件处理程序。该事件是文档就绪事件。

第二步是创建一个超时,在指定的延迟后执行所需的功能。

var scrollTimeout = null;
var jDocument = $(document);
jDocument.ready(function () { //An event fired when the document finishes loading.
    scrollTimeout = setTimeout(function () { //A function the will be executed after 2000ms
        jDocument.scrollTop(); //your code goes here.
    }, 2000);
});