HTML文档中的滚动位置 - 如何确定

时间:2021-01-13 01:49:09

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

想象一下HTML文档,其中包含很长的段落列表。用户使用滚动条向下滚动文档52%。

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

如何捕获文档为52%或第100段或其他文档?

1 个解决方案

#1


6  

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

使用下面的两个功能,您可以确定您需要什么。

// getPageScroll() by quirksmode.com
// 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.com
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
}

#1


6  

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

使用下面的两个功能,您可以确定您需要什么。

// getPageScroll() by quirksmode.com
// 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.com
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
}