元素属性和元素方法都通过 elem.属性
或 elem.方法
的方式使用,window属性通过 window.属性
的方式使用,document属性则通过document调用。
<script>
1,元素视图属性
offsetWidth 水平方向 width + 左右padding + 左右border-width
offsetHeight 垂直方向 height + 上下padding + 上下border-width
clientWidth 水平方向 width + 左右padding - 滚动条width(盒子有滚动条时)
clientHeight 垂直方向 height + 上下padding
offsetTop 获取当前元素到 定位父节点 的top方向的距离
offsetLeft 获取当前元素到 定位父节点 的left方向的距离
scrollWidth 元素内容真实的宽度,内容不超出盒子高度时为盒子的clientWidth
scrollHeight 元素内容真实的高度,内容不超出盒子高度时为盒子的clientHeight
2,Window视图属性(低版本IE浏览器[<IE9]不支持,亲测包含滚动条)
innerWidth 浏览器窗口宽度
innerHeight 浏览器窗口高度
3,Document文档视图
*(低版本IE的innerWidth、innerHeight的代替方案)
document.documentElement.clientWidth || document.body.clientWidth 网页可视区宽度(不包括滚动条,边框)
document.documentElement.clientHeight || document.body.clientHeight 网页可视区高度(同上)
document.documentElement.offsetWidth || document.body.offsetWidth 网页可视区宽度(不包括滚动条)
document.documentElement.offsetHeight 获取整个文档的高度(包含body的margin)
document.body.offsetHeight 获取整个文档的高度(不包含body的margin)
document.body.scrollWidth 网页正文全文宽
document.body.scrollHeight 网页正文全文高
document.documentElement.scrollTop 返回文档的滚动top方向的距离(当窗口发生滚动时值改变)
document.documentElement.scrollLeft 返回文档的滚动left方向的距离(当窗口发生滚动时值改变)
4,元素方法
(1) getBoundingClientRect() 获取元素到body
bottom: 元素底边(包括border)到可视区最顶部的距离
left: 元素最左边(不包括border)到可视区最左边的距离
right: 元素最右边(包括border)到可视区最左边的距离
top: 元素顶边(不包括border)到可视区最顶部的距离
height: 元素的offsetHeight
width: 元素的offsetWidth
x: 元素左上角的x坐标
y: 元素左上角的y坐标
(2) scrollIntoView() 让元素滚动到可视区
</script>