Vue動態獲取width的方法


1、html
<div ref="getheight"></div> <br><br>

2、JavaScript
// 獲取高度值 (內容高+padding+邊框)
let height= this.$refs.getheight.offsetHeight; 
// 獲取元素樣式值 (存在單位)
let height = window.getComputedStyle(this.$refs.getheight).height;
 
//獲取元素內聯樣式值(非內聯樣式無法獲取)
let width= this.$refs.getheight.offsetWidht;

3,javascript中獲取dom元素高度和寬度
javascript中獲取dom元素高度和寬度的方法如下:

網頁可見區域寬: document.body.clientWidth
網頁可見區域高: document.body.clientHeight
網頁可見區域寬: document.body.offsetWidth (包括邊線的寬)
網頁可見區域高: document.body.offsetHeight (包括邊線的高)
網頁正文全文寬: document.body.scrollWidth
網頁正文全文高: document.body.scrollHeight
網頁被卷去的高: document.body.scrollTop
網頁被卷去的左: document.body.scrollLeft

對應的dom元素的寬高有以下幾個常用的:

元素的實際高度:document.getElementById("div").offsetHeight
元素的實際寬度:document.getElementById("div").offsetWidth
元素的實際距離左邊界的距離:document.getElementById("div").offsetLeft
元素的實際距離上邊界的距離:document.getElementById("div").offsetTop

 


免責聲明!

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



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