Blogger Information
Blog 12
fans 0
comment 0
visits 11063
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
盒模型常用元素属性之大小、位置
amin
Original
716 people have browsed it
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>盒模型常用元素属性之大小、位置</title>
  9. <style>
  10. .box{
  11. width: 300px;
  12. height: 300px;
  13. background: #7FFFD4;
  14. position: relative;
  15. border: 2px solid #000;
  16. padding: 10px;
  17. box-sizing: border-box;
  18. /*
  19. box-sizing:border-box boxWidth=content+padding+border
  20. box-sizing:content-box boxWidth=content
  21. */
  22. }
  23. .box1{
  24. width: 100px;
  25. height: 100px;
  26. background: #7f7f7f;
  27. /*垂直水平居中*/
  28. position: absolute;
  29. margin: auto;
  30. top: 0;
  31. bottom: 0;
  32. left: 0;
  33. right: 0;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div class="box">
  39. <div class="box1"></div>
  40. </div>
  41. <script>
  42. const html = document.documentElement;//返回文档的根节点html
  43. console.log(html.clientWidth);//clientWidth = contentWidth + 左右padding
  44. console.log(html.clientHeight);//clientHeight = contentHeight + 上下padding
  45. /*
  46. *下面两个一般不用
  47. * clientTop = borderTop 上边框宽度
  48. * clientLeft = borderLeft 左边框宽度
  49. */
  50. const box = document.querySelector(".box");
  51. console.log("box.clientWidth:" + box.clientWidth);
  52. console.log("box.offsetWidth:" + box.offsetWidth);
  53. /*
  54. * offsetWidth = contentWidth + 左右padding + 左右border
  55. * offsetHeight = contentHeight + 上下padding + 上下border
  56. * offsetTop: 当前元素上边框(外边缘)到定位父级(没有定位父级就是body)上边框(内边缘)的距离
  57. * offsetLeft:当前元素左边框(外边缘)到定位父级(没有定位父级就是body)左边框(内边缘)的距离
  58. */
  59. html.style.height = 2000 + "px";
  60. console.log(html.scrollHeight);//当前文档的实际高度 同理scrollWidth
  61. console.log(html.clientHeight);//当前文档可视区高度
  62. console.log(html.scrollTop);//当前文档可视区顶部到文档顶部的距离 同理scrollLeft
  63. </script>
  64. </body>
  65. </html>
Correcting teacher:WJWJ

Correction status:qualified

Teacher's comments:写的简单了点,建议加上效果图方便查看。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post