scroll bar与document 宽度与js的关系

一个重要的细节

首先从浏览器模式来说

如果document有responsive meta,在普通模式下

scroll bar是附加在文档的最左边

而在device mode下

scroll bar 是漂浮在文档上面的

不获知这点,接下来便无从谈起

----------------------------------------------------

1.对window来说

只有innerWidth和outterWidth,没有width属性,没有offsetWidth属性

其中innerWidth=window内整个document的宽度(包含scrollbar的宽度!!)

outterWidth为整个窗口的宽度

按下F12你就知道~

2.对window.document来说

这是个天煞孤星

window.document.width/innerWidth/outerWidth/offsetWidth都不存在

3.对window.document.body来说

这个专一的家伙只有offsetWidth可以选

并且这就是整个body的宽度(不包括scrollbar)

小结论

在没有水平滚动条的情况下

垂直滚动条的宽度=window.innerWidth-document.body.offsetWidth

原文地址:https://www.cnblogs.com/AlexNull/p/5070501.html