原生js獲取元素的各種位置(大全)


 

加給元素:

  • offsetLeft (距離定位父級的距離)
  • offsetTop (距離定位父級的距離)
  • offsetWidth (可視寬度)
  • offsetHeight (可視高度)
  • clientLeft (左邊框寬度)
  • clientTop (上邊框寬度)
  • clientWidth(width + padding)
  • clientHeight(height + padding)
  • scrollTop(縱向滾動距離)
  • scrollLeft(橫向滾動距離)
  • scrollWidth(內容寬度)
  • scrollHeight(內容高度)

getBoundingClientRect ( ) 返回值:對象 有6個屬性

  • left(元素左側相對於可視區左上角的距離)
  • right(元素右側相對於可視區左上角的距離)
  • top(元素上邊相對於可視區左上角的距離)
  • bottom(元素下邊相對於可視區左上角的距離)
  • width(可視寬度)
  • height(可視高度)

獲取可視區寬高:

  • window.innerWidth
  • window.innerHeight
  • document.documentElement.clientWidth
  • document.documentElement.clientHeight

屏幕寬高:

  • window.screen.width
  • window.screen,height

獲取文檔寬高:

  • document.body.clientWidth
  • document.body.clientHeight
  • document.documentElement.scrollWidth
  • document.documentElement.scrollHeight
  • document.body.scrollWidth(如果內容寬度超過一屏,得到文檔寬度;如果內容小於一屏,得到一屏的寬度)
  • document.body.scrollHeight (如果內容高度超過一屏,得到文檔高度;如果內容小於一屏,得到一屏的高度)

獲取滾動條距離:

  • document.body.scrollTop
  • document.body.scrollLeft
  • window.scrollY
  • window.scrollX
  • document.documentElement.scrollTop
  • document.documentElement.scrollLeft
  • window.pageYOffset
  • window.pageXOffset


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM