最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check the scrollTop() of document after few seconds - Stack Overflow

programmeradmin1浏览0评论

I want to check $(document).scrollTop() two seconds after the page loads. How can I do that? Can somebody help me? I am newbie in jQuery?

I tried this, but it does not work:

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

I want to check $(document).scrollTop() two seconds after the page loads. How can I do that? Can somebody help me? I am newbie in jQuery?

I tried this, but it does not work:

$(document).delay(2000).scrollTop()
Share Improve this question edited Dec 24, 2014 at 13:36 Mr. Polywhirl 48.9k12 gold badges93 silver badges145 bronze badges asked Dec 24, 2014 at 13:34 nowikonowiko 2,5777 gold badges40 silver badges87 bronze badges 3
  • 6 setTimeout is your fiend. – Ram Commented Dec 24, 2014 at 13:36
  • use setTimeout rather than delay – akaBase Commented Dec 24, 2014 at 13:36
  • 1 Remember that .delay() can only be used for queued effects in jQuery. It is not intended as a replacement for setTimeout(). – Terry Commented Dec 24, 2014 at 13:40
Add a ment  | 

2 Answers 2

Reset to default 9

You can do it using the setTimeout() function after the document.ready was triggered:

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

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

});

The first step is to fire an event handler when the document is loaded. That event is the document ready event.

The second step is to create a timeout that will execute the required function after a specified delay.

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);
});
发布评论

评论列表(0)

  1. 暂无评论