06.其他样式操作的属性.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #box1 {
  10. width: 100px;
  11. height: 100px;
  12. background-color: red;
  13. padding: 10px;
  14. border: 10px solid yellow;
  15. }
  16. #box2{
  17. padding: 100px;
  18. background-color: #bfa;
  19. }
  20. #box4{
  21. width: 200px;
  22. height: 300px;
  23. background-color: #bfa;
  24. overflow: auto;
  25. }
  26. #box5{
  27. width: 450px;
  28. height: 600px;
  29. background-color: yellow;
  30. }
  31. </style>
  32. <script>
  33. window.onload = function () {
  34. var box1 = document.getElementById("box1");
  35. var btn01 = document.getElementById("btn01");
  36. btn01.onclick = function () {
  37. /**
  38. * clientWidth
  39. * clientHeight
  40. * -这两个属性可以获取元素的可见宽度和高度
  41. * -这些属性都是不带px的,返回都是一个数字,可以直接进行计算
  42. * -会获取元素宽度和高度,包括内容区和内边距(不包括边框)
  43. * -这些属性都是只读的,不能修改
  44. */
  45. //alert(box1.clientWidth);
  46. //alert(box1.clientHeight);
  47. // box1.clientHeight = 200;
  48. /**
  49. * offsetWidth
  50. * offsetHeight
  51. * -获取元素的整个的宽度和高度,包括内容区、内边距和边框
  52. */
  53. // alert(box1.offsetWidth);
  54. /**
  55. * offsetParent
  56. * -可以用来获取当前元素的定位父元素
  57. * -会获取到离当前元素最近的开启了定位的祖先元素
  58. * 如果所有的祖先元素都没有开启定位,则返回body
  59. */
  60. var op = box1.offsetParent;
  61. // alert(op);
  62. // alert(op.id);
  63. /**
  64. * offsetLeft
  65. * -当前元素相对于其定位父元素的水平偏移量
  66. * offsetTop
  67. * -当前元素相对于其定位父元素的垂直偏移量
  68. */
  69. // alert(box1.offsetLeft);
  70. /**
  71. * scrollWidth
  72. * scrollHeight
  73. * -可以获取元素整个滚动区域的宽度和高度
  74. */
  75. var box4 = document.getElementById("box4");
  76. // alert(box4.clientHeight);//300,可见区域的高度
  77. // alert(box4.scrollHeight);//600,滚动区域的高度
  78. // alert(box4.scrollWidth);//600,滚动区域的宽度
  79. /**
  80. * scrollLeft
  81. * -可以获取水平滚动条滚动的距离
  82. * scrollTop
  83. * -可以获取垂直滚动条滚动的距离
  84. */
  85. alert(box4.scrollLeft);
  86. alert(box4.scrollTop);
  87. alert(box4.clientHeight);//283
  88. //当满足scrollHeight-scrollTop==clientHeight
  89. //说明垂直滚动条滚动到底了
  90. alert(box4.scrollHeight-box4.scrollTop);//283
  91. //当满足scrollWidth-scrollLeft==clientWidth
  92. //说明水平滚动条滚动到底了
  93. }
  94. };
  95. </script>
  96. </head>
  97. <body id="body">
  98. <button id="btn01">点一下</button>
  99. <div id="box4">
  100. <div id="box5"></div>
  101. </div>
  102. <br /><br />
  103. <!-- <div id="box3" style="position: relative;">
  104. <div id="box2" style="position: relative;">
  105. <div id="box1"></div>
  106. </div>
  107. </div> -->
  108. <div id="box3">
  109. <div id="box2" style="position: relative;">
  110. <div id="box1"></div>
  111. </div>
  112. </div>
  113. </body>
  114. </html>