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

javascript - Scroll Position in HTML document - How to determine - Stack Overflow

programmeradmin0浏览0评论

Imagine an HTML documetn with a long long list of paragraphs. The user scrolls 52% down the document with the scroll bar.

How can I capture that the document is 52% or at paragraph 100 or other metic?

Imagine an HTML documetn with a long long list of paragraphs. The user scrolls 52% down the document with the scroll bar.

How can I capture that the document is 52% or at paragraph 100 or other metic?

Share Improve this question asked Nov 8, 2010 at 4:06 Ian VinkIan Vink 68.9k107 gold badges353 silver badges567 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Use the two functions below and you can determine what you need.

// getPageScroll() by quirksmode.
// use getPageScroll()[0] for horizontal scrolled amount
// use getPageScroll()[1] for vertical scrolled amount
function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll,yScroll)
}

// Adapted from getPageSize() by quirksmode.
function getPageHeight() {
    var windowHeight
    if (self.innerHeight) { // all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }
    return windowHeight
}
发布评论

评论列表(0)

  1. 暂无评论