Height、width系列

1、scrollHeight、clientHeight、offsetHeight

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.
The Element.clientHeight read-only property is zero for elements with no CSS or inline layout boxes, otherwise it's the inner height of an element in pixels, including padding but not the horizontal scrollbar height, border, or margin.
The HTMLElement.offsetHeight read-only property is the height of the element including vertical padding and borders, as an integer.

1、如果没有滚动条,scrollHeight 的高度跟 clientHeight 的高度相同。
利用这个特点,可以判断这个元素有没有出现(垂直)滚动条

if (Element.scrollHeight > Element.clientHeight) {
    // 可以解决模态框出现/消失的抖动
    // document.documentElement.style.marginRight = `15px`
    //  document.documentElement.style.overflow = `15px`
}

2、三者都包括 padding , 但 offsetHeight 包括 borderscrollHeightclientHeight 不包括border
3、上面三个值都是整数 而且都是只读属性 ,获取更精确的数值,可以使用
element.getBoundingClientRect()

2、window.innerWidth、window.outerWidth

window.innerWidth is width of the browser window viewport including, if rendered, the vertical scrollbar
Window.outerWidth gets the width of the outside of the browser window. It represents the width of the whole browser window including sidebar (if expanded), window chrome and window resizing borders/handles.

  • 1、这两个是 window 的属性,前者是视口的高度(不准确),后者是这个浏览器的高度。

  • 2、document.documentElement html文档的根元素
    document.documentElement.clientWidth 表示的是视口的宽度,并非<html>的宽度。区别可以通过设置

     html {
       width: 10%;
     }
     // 用这个CSS很容易理解viewport和<html>元素的区别:
     // viewport处于 `html`的外层,是无法css设置的
    

    document.documentElement.clientWidthwindow.innerWidth 的区别在于是否包含滚动条。window.innerWidth 是包含滚动条的。所以👆说不准确。 document.documentElement.clientWidth才是真正的视口的宽度。

    测量<html>可以用

    document.documentElement.offsetWidth/Height
    
  • 3、window.resize() 是可以改变 window 的大小的。

参考文章
1、A tale of two viewports — part one

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • Window和document对象的区别 window对象window对象表示浏览器中打开的窗口window对象是...
    FConfidence阅读 2,275评论 0 5
  • 为移动设备添加 viewport 参数说明:content参数 释义width viewport 宽度(数值/de...
    _Charles阅读 457评论 0 0
  • 01 昨天和闺蜜在网上聊天,每次都会聊到的一个...
    杨亦楊阅读 280评论 0 1
  • 醒着幻想着的东西 我也将它称作梦 和熟睡后做的梦一样 都会自然的在脑海中泛起漪涟 梦,不是那现实,却由那现实而生,...
    顾城的诗阅读 189评论 0 0