offset的屬性的介紹

2021-08-16 05:54:53 字數 1881 閱讀 7608

在獲取元素的高寬等屬性時,dom提供了這些api提供給我們下面進行介紹(只能檢視,無法修改,修改用 .style.css):

這是我建立的樣例,方便檢視

.box

.box p

現在利用js語言對dom定義的offset進行測試

var obox = document.queryselector(".box");//藍色邊框的盒子

var op = document.queryselector(".box p");//粉紅色邊框的p

1.1.offsetparent

是指自己祖先元素中,離自己最近的已經定位的元素,如果自己的祖先元素中,沒有任何盒子進行了定位,那麼offsetparent物件就是body。

console.log(op.offsetparent); //...

1.2.offsetleft

乙個元素的offsetleft值,就是這個元素左邊框外,到自己的offsetparent物件的左邊框內的距離。

console.log(op.offsetleft);//50
1.3.offsettop

乙個元素的offsettop值,就是這個元素上邊框外,到自己的offsetparent物件的上邊框內的距離。

console.log(op.offsettop);//50
2.offsetwidth offsetheight

offwidth是自己的width+左右padding+左右border的寬度

offheight是自己的height+上下padding+上下border的寬度

console.log(op.offsetheight);//120

console.log(op.offsetwidth);//120

console.log(obox.offsetheight);//340

console.log(obox.offsetwidth);//840

3.clientwidth 和 clientheight

clientwidth值與offsetwidth區別是width + padding,相比與offsetwidth少計算了border

clientheight同樣是這樣

4.scrollheight

獲取元素內容實際高度,也就是內容的實際高度。有沒有隱藏都沒關係

p
哈哈哈哈啊哈哈哈哈啊哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊哈哈

哈哈哈哈啊哈哈哈哈啊哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊哈哈

哈哈哈哈啊哈哈哈哈啊哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊哈哈

哈哈哈哈啊哈哈哈哈啊哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊哈哈

var op  = document.queryselector("p");

console.log(op.offsetwidth);//220

console.log(op.clientwidth);//200

console.log(op.scrollheight);//449

DOM 中的 offset 屬性

與當前元素最近的經過定位 position 不等於 static 的父級元素,主要分為下列幾種情況 元素自身有fixed定位,offsetparent的結果為null 當元素自身有fixed固定定位時,我們知道固定定位的元素相對於視口進行定位,此時沒有定位父級,offsetparent的結果為nul...

PC端網頁特效 offset系列屬性

offset概述 offset翻譯是偏移量,使用offset系列相關屬性可以動態的得到該元素的位置 偏移 大小等。獲得元素距離帶有定位父元素的位置 獲得元素自身的大小 寬度高度 注意 返回的數值都不帶單位 offset系列屬性 作用 element.offsetparent 返回作為該元素帶有定位的...

GridView的屬性介紹

舉個例子來說吧 你將乙個student表繫結到grid上 這個表裡有一些字段 包括id 姓名 學號 等等等等 grid的datakey 屬性設定了 datakey 學號 下邊開始解釋問題 gridview1.datakeys是什麼?gridview有乙個屬性是datakey 當將資料表繫結到grid...