Javascript 为什么滚动百分比超过100

Javascript 为什么滚动百分比超过100,javascript,scroll,Javascript,Scroll,我使用这个方法来计算滚动百分比(在网上阅读了一堆帖子之后)。但是,当我滚动到最底部时,滚动百分比将变为>100%。这些数值是: 滚动值:滚动高度:3405滚动条高度:1408滚动顶部:1997.333374023375滚动百分比:100.01669374178455 我想知道为什么会发生这种情况,如果我的方法是错误的 谢谢 // This is the container with the scrolbar. We are using this element as we want th

我使用这个方法来计算滚动百分比(在网上阅读了一堆帖子之后)。但是,当我滚动到最底部时,滚动百分比将变为>100%。这些数值是:

滚动值:滚动高度:3405滚动条高度:1408滚动顶部:1997.333374023375滚动百分比:100.01669374178455

我想知道为什么会发生这种情况,如果我的方法是错误的

谢谢

    // This is the container with the scrolbar. We are using this element as we want the inner div. document.documentElement is the full element
    const scrollingElement: HTMLElement = document.getElementById(personalInsightsContainerId);

    // If no scrollingElement is present, do not do anything.
    if (scrollingElement) {
        // The Element.scrollTop property gets or sets the number of pixels that the content of an element is scrolled upward.
        // An element's scrollTop is a form of distance measurement regarding an element's top to its topmost visible content.
        // When an element content does not generate a vertical Scrollbar, then its scrollTop value defaults to 0.
        const scrollTop: number = scrollingElement.scrollTop;

        // The difference b/w scrolling point.
        const scrollingDifference: number = scrollTop - this.lastScrollNumber;

        // We want to scroll if: a) there is scrollElement defined b) either the scrolling is first time or the scrollDifference is > 100.
        if (this.lastScrollNumber === 0 || scrollingDifference > 100) {
            // The Element.scrollHeight read-only property is a measurement of the height of an element's content,
            // Including content not visible on the screen due to overflow.
            const scrollHeight = scrollingElement.scrollHeight;

            // The HTMLElement.offsetHeight read-only property is the height of the element including vertical padding and borders, as an integer.
            const scrollBarHeight = scrollingElement.offsetHeight;

            if (scrollHeight && scrollBarHeight) {
                this.lastScrollNumber = scrollTop;

                 const totalHeight = scrollHeight - scrollBarHeight;
                 const scrollPercentage = totalHeight > 0 ? (scrollTop / totalHeight) * 100 : -1;
        }
    }

尝试使用clientHeight而不是offsetHeight

您使用的浏览器是什么?在chrome/FF元素中,scrollTop始终为整数值。尝试使用constScrollTop:number=Math.floor(scrollingElement.scrollTop)

基本上,您在
(scrollTop/totalHeight)
中得到的值略大于1。这是一个具体的示例,我可以在其中重新编程并获得值。但也有其他情况下,值要大得多-这就是为什么它是一个问题。在我的例子中,IE6+clientHeight与offsetheight相同。我使用的是Chrome和Edge。