在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ document.documentElement.scrollTop和docum

document.documentElement.scrollTop和document.body.scrollTop同時(shí)為0?

什么情況下會(huì)使document.documentElement.scrollTop和document.body.scrollTop同時(shí)為0?

別和我說什么DTD的問題,我想問的是他倆同時(shí)為0了,之前使用

document.body.scrollTop || document.documentElement.scrollTop

是可以獲取到屏幕滾動(dòng)高度的,但是項(xiàng)目多人參與,不知道改了哪里,現(xiàn)在這兩個(gè)會(huì)去到的值都變成0了。

回答
編輯回答
安于心

受上回答啟發(fā)確實(shí)跟css有關(guān),
但是:

html {
    height: 100%;
    overflow: scroll;
}
這樣寫是獲取不到值的
html {
    height: 100%;
    overflow: visible;
}
這樣寫就可以獲取到值了,所以跟html的高度是否是100%并無關(guān)系
2018年1月31日 14:42
編輯回答
尕筱澄

和你的頁面布局有關(guān)。
正常情況下滾動(dòng)條是屬于 html 的,頁面撐開可以正常獲取document.documentElement.scrollTop。
在滾動(dòng)條屬于 htmlbody 的情況下document.body.scrollTop || document.documentElement.scrollTop能正常拿到相應(yīng)值。

如果都為0,那說明:

  1. 當(dāng)前滾動(dòng)條位置就是在頂部。
  2. 沒有產(chǎn)生滾動(dòng)。
  3. 你當(dāng)前的滾動(dòng)條不再屬于 html 或者 body。
  4. 其它我沒想到的= =。

比如以下這種結(jié)構(gòu)對應(yīng)3:

<!DOCTYPE html>
<html>
    <body>
        <div class="wrapper">
            <div class="main"></div>
        </div>
    </body>
</html>

<style>
html, body, .wrapper {
    width: 100%;
    height: 100%;
    overflow: auto;
}
.main {
    height: 10000px;
}
</style>

這樣.main撐開的滾動(dòng)條其實(shí)是屬于.wrapper的。只有wrapper.scrollTop可以獲取相應(yīng)的值。
document.body.scrollTop || document.documentElement.scrollTop自然始終為0。

2017年7月10日 00:07
編輯回答
尛曖昧

這也太巧了吧,我今天也是這樣的情況,本來上拉加載好好的,不知道為什么突然就掛了,定位發(fā)現(xiàn)是

document.body.scrollTop

始終為0
我一開始也以為是css造成了,但是我也沒改啊0.0,最后放棄了,將這個(gè)獲取方法更兼容的寫了下就好了。

let scrollTop = window.pageYOffset
    || document.documentElement.scrollTop
    || document.body.scrollTop
    || 0;
2018年1月19日 05:56