前端干货搜集让前端飞技术干货

JS中client/offset/scroll等的宽高解析

2017-07-21  本文已影响494人  楼心漫
眼花缭乱

原文地址:→传送门

window相关宽高属性

1. window.outerHeight (窗口的外层的高度) / window.outerWidth (窗口的外层的宽度)

参考链接:
window.outerHeight -MDN
window.outerWidth-MDN

2. window.innerHeight (浏览器窗口的视口的高度)和 window.innerWidth

参考链接:
window.innerHeight - MDN
window.innerWidth - MDN

3. window.screen.widthwindow.screen.height

注:在获取值时可不加window.前缀

参考链接:
window.screen - MDN
window.screen - MDN

4. window.screen.availHeightwindow.screen.availWidth

参考链接:
window.screen - MDN
window.screen - MDN

5. window.screenTopwindow.screenLeft

参考链接:
window.screenTop
window.screenLeft

document下面的相关属性(offset/client/scroll)

与client相关属性

6. document.body.clientWidthelement.clientWidth

参考链接:
Element.clientWidth - MDN

7. document.body.clientHeightelement.clientHeight

参考链接:
element.clientHeight - MDN

8. document.body.clientLeftelement.clientLeft

参考链接:
Element.clientLeft - MDN

9. document.body.clientTopelement.clientTop

参考链接:
Element.clientTop

与offset相关属性

这一对属性指的是元素的border+padding+content的宽度和高度

10. document.body.offsetWidthelement.offsetWidth

参考链接:
HTMLElement.offsetWidth

11. document.body.offsetHeightelement.offsetHeight

参考链接:
HTMLElement.offsetHeight

12. HTMLElement.offsetLeftHTMLElement.offsetTop

这两个属性是基于offsetParent的

offsetLeft=(offsetParent的padding-left)+(当前元素的margin-left)
offsetLeft=(offsetParent的margin-left)+(offsetParent的border宽度)+(offsetParent的padding-left)+(当前元素的margin-left)
offsetLeft=(offsetParent的margin-left)+(当前元素的margin-left)+(offsetParent的padding-left)

参考链接:
HTMLElement.offsetLeft
HTMLElement.offsetTop

与scroll相关属性

13. document.body.scrollWidthdocument.body.scrollHeight

body与普通element不一样

scrollWidth = 浏览器窗口宽度
scrollHeight = 浏览器窗口高度
scrollWidth = 给定的宽度 + body所有padding + body所有margin + body所有border
scrollHeight = 给定的高度 + body所有padding + body所有margin + body所有border
scrollWidth = 内容的宽度 + body所有padding + body所有margin + body所有border
scrollHeight = 内容的高度 + body所有padding + body所有margin + body所有border

参考链接:
Element.scrollWidth

14. element.scrollWidthelement.scrollHeight
scrollWidth = clientWidth = 元素宽度 + padding和 
scrollHeight = clientHeight = 元素高度 + padding和
scrollWidth = 实际内容的宽度 + padding和 
scrollHeight  = 实际内容的高度 + padding和
15. document.body.scrollTopdocument.body.scrollLeft
16. window.scrollYwindow.scrollX

event中的五种坐标

  1. clientXclientY:相对于浏览器(可视区左上角0,0)的坐标

  2. screenXscreenY:相对于设备屏幕左上角(0,0)的坐标

  3. offsetXoffsetY:相对于事件源左上角(0,0)的坐标

  4. pageXpageY:相对于整个网页左上角(0,0)的坐标

参考来源:
上一篇 下一篇

猜你喜欢

热点阅读