document.documentElement.scrollTop和document.body.

什么情况下会使document.documentElement.scrollTop和document.body.scrollTop同时为0?

别和我说什么DTD的问题,我想问的是他俩同时为0了,之前使用

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

是可以获取到屏幕滚动高度的,但是项目多人参与,不知道改了哪里,现在这两个会去到的值都变成0了。


杨__羊羊
浏览 823回答 2
2回答

拉莫斯之舞

受上回答启发确实跟css有关,但是:html {    height: 100%;    overflow: scroll;}这样写是获取不到值的html {    height: 100%;    overflow: visible;}这样写就可以获取到值了,所以跟html的高度是否是100%并无关系

尚方宝剑之说

和你的页面布局有关。正常情况下滚动条是属于 html 的,页面撑开可以正常获取document.documentElement.scrollTop。在滚动条属于 html 或 body 的情况下document.body.scrollTop || document.documentElement.scrollTop能正常拿到相应值。如果都为0,那说明:当前滚动条位置就是在顶部。没有产生滚动。你当前的滚动条不再属于 html 或者 body。其它我没想到的= =。比如以下这种结构对应3:<!DOCTYPE html><html>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div class="wrapper">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="main"></div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body></html><style>html, body, .wrapper {&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; overflow: auto;}.main {&nbsp; &nbsp; height: 10000px;}</style>这样.main撑开的滚动条其实是属于.wrapper的。只有wrapper.scrollTop可以获取相应的值。document.body.scrollTop || document.documentElement.scrollTop自然始终为0。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript