js監聽視窗變化動態改變div大小
阿新 • • 發佈:2018-12-06
js監聽視窗變化動態改變div大小
function getHeight(val) {
this.processHeight=(window.innerHeight || document.documentElement.clientHeigh)-210 +'px';
}
getHeight(1);
//通過註冊resize監聽器,實現對視窗大小的監聽
window.addEventListener('resize', e => { this.getHeight(1); }, false);
//溢位resize監聽器
//window.removeEventListener('resize', e => { this.getHeight(1); });
獲取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