本文最后更新于 2024-03-23T16:32:21+00:00
                  
                  
                
              
            
            
              
                
                屏幕相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   |  window.screen = {   availHeight: 950,    availLeft: 0,    availTop: 32,    availWidth: 1512,    height: 982,    width: 1512,  };
 
  window.screenLeft === window.screenX;
 
  window.screenTop === window.screenY;
 
  | 
 
浏览器相关
1 2 3 4 5 6 7 8
   | window.outerWidth;  window.outerHeight; 
 
  window.innerWidth;
 
  window.innerHeight;
 
  | 
 
网页相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   | 
  document.documentElement.clientWidth
 
 
  document.documentElement.clientHeight
 
 
 
  document.documentElement.scrollWidth
 
 
 
  document.documentElement.scrollHeight
 
  document.documentElement.scrollTop
 
  document.documentElement.scrollLeft
 
  | 
 
元素相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   | 
 
  document.某个元素.clientWidth
 
 
 
 
  document.某个元素.clientHeight
 
 
 
 
  document.某个元素.scrollWidth
 
 
 
 
  document.某个元素.scrollHeight
 
  document.某个元素.scrollTop
 
  document.某个元素.scrollLeft
  document.某个元素.offsetWidth  document.某个元素.offsetHeight  document.某个元素.offsetLeft  document.某个元素.offsetTop 
 
  | 
 
举个🌰
1 2 3 4 5 6 7 8 9 10 11 12
   | <div id="example">   ...Text... </div> <style>   #example {     width: 300px;     height: 200px;     border: 25px solid #E8C48F;     padding: 20px;     overflow: auto;   } </style>
 
  | 
 



对应的值为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   |  clientWidth = 323 = content width(283.2) + 左右 padding(20 * 2) =  width(300) + 左右 padding(20 * 2) - 竖向滚动条宽度 17(若有)
  scrollWidth = 323 = clientWidth
  offsetWidth = 390 = width(300) + 左右 padding(20 * 2) + 左右 border(25 * 2)
 
  clientHeight = 240 = content height(200) + 左右 padding(20 * 2) =  height(200) + 左右 padding(20 * 2) - 横向滚动条高度 17(若有,本次无,为 0)
  scrollHeight  = 435
  offsetHeight  = 290 = height(200) + 左右 padding(20 * 2) + 左右 border(25 * 2)
 
  offsetTop = 看实际情况 offsetLeft = 看实际情况 clientTop = 25 = border-top clientLeft = 25 = border-left
 
  | 
 
快速获取这些值:
1 2 3 4 5 6 7 8 9 10 11
   | document.某个元素.getBoundingClientRect() {   bottom: 456,    height: 290,    left: 0,    right: 390,    top: 166,    width: 390,    x: 0,    y: 166  }
 
  | 
 
实际场景
1、判断元素是否在可视区域内
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | 
  const { clientWidth, clientHeight } = document.documentElement
 
  const { top, left, right, bottom } = document.某个元素.getBoundingClientRect()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  | 
 
                
               
            
            
            
              
              
  
  
    
      10、JS 获取各种距离的方法有哪些?
      https://mrhzq.github.io/职业上一二事/前端面试/每日一题/10、JS 获取各种距离的方法有哪些?/