视口的宽高与滚动高度_html/css_WEB-ITnose

php中文网
发布: 2016-06-21 08:50:22
原创
1819人浏览过

很多场景下会需要在javascript中获取窗口或dom元素的宽高,以及滚动高度。 例如:实现滚动效果、创建全屏布局、动态绝对定位等等。 本文就来介绍相关的dom api: window.innerheight , window.outerheight , clientheight , offsetheight , scrollheight , scrolltop 等(当然每个属性都有对应的width)。

整个窗口大小

innerHeight与outerHeight

通过 window.innerHeight 和 window.outerHeight 可以得到整个窗口的高度。其中:

  • innerHeight 是DOM视口的大小,包括滚动条。
  • outerHeight 是整个浏览器窗口的大小,包括窗口标题、工具栏、状态栏等。

把 Height 改为 Width 同样有效,分别是 innerWidth 和 outerWidth 。

注意:IE8及以下不支持本节介绍的 window.innerHeight 等属性。

立即学习前端免费学习笔记(深入)”;

clientHeight

在不支持 window.innerHeight 的浏览器中,可以读取 documentElement 和 body 的高度, 它们的大小和 window.innerHeight 是一样的(其实不太一样,见下一小节)。

document.documentElement.clientHeightdocument.body.clientHeight
登录后复制

其中 documentElement 是文档根元素,就是 标签。

The Document.documentElement read-only property returns the Element that is the root element of the document (for example, the element for HTML documents). – MDN

body 顾名思义就是

标签了。这两种方式兼容性较好,可以一直兼容到IE6,就是写起来费劲。

最佳实践

既然获取窗口大小存在浏览器兼容问题,在实践中通常使用下面的代码来兼容所有浏览器:

var height = window.innerHeight    || document.documentElement.clientHeight    || document.body.clientHeight;
登录后复制

事实上后两种方式获取的高度和 window.innerHeight 是不一样的,这3个属性的值逐个变小。 具体说来, window.innerHeight 包括整个DOM:内容、边框以及滚动条。

  • documentElement.clientHeight 不包括整个文档的滚动条,但包括 元素的边框。
  • body.clientHeight 不包括整个文档的滚动条,也不包括 元素的边框,也不包括 的边框和滚动条。

其实使用 offsetHeight 作为Fallback要比 clientHeight 更好,更多的讨论请见下文。

滚动高度

在使用JavaScript控制页面滚动时(例如回到顶部),需要知道页面当前滚动到了哪里,以及滚动到的目标是哪里。 这便是滚动高度。这涉及到4个DOM属性, clientHeight , offsetHeight , scrollHeight , scrollTop 。

百度·度咔剪辑
百度·度咔剪辑

度咔剪辑,百度旗下独立视频剪辑App

百度·度咔剪辑 3
查看详情 百度·度咔剪辑

所有DOM元素都有上述4各属性,只需要给它固定大小并设置 overflow:scroll 即可表现出来。

  • clientHeight : 内部可视区域大小。

    returns the inner height of an element in pixels, including padding but not the horizontal scrollbar height, border, or margin

  • offsetHeight :整个可视区域大小,包括border和scrollbar在内。

    is a measurement which includes the element borders, the element vertical padding, the element horizontal scrollbar (if present, if rendered) and the element CSS height.

  • scrollHeight :元素内容的高度,包括溢出部分。

    is a measurement of the height of an element’s content including content not visible on the screen due to overflow

  • scrollTop :元素内容向上滚动了多少像素,如果没有滚动则为0。

    the number of pixels that the content of an element is scrolled upward.

如下图:

对应的横向属性为: clientWidth , offsetWidth , scrollWidth , scrollLeft 。

参考阅读

  • Window - W3School: http://www.w3school.com.cn/js/js_window.asp
  • Window.innerHeight - MDN: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/innerHeight
  • Element.clientWidth - MDN: https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight
  • Document.documentElement - MDN: https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
  • Document.body - MDN: https://developer.mozilla.org/en-US/docs/Web/API/Document/body
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号