JavaScript中clientWidth,offsetWidth,scrollWidth的區(qū)別
一、概念
它們都是Element的屬性,表示元素的寬度:
Element.clientWidth 內(nèi)容+內(nèi)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容
Element.scrollWidth 內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容
Element.offsetWidth 元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件
二、舉例
1、僅有橫向滾動(dòng)條的情況
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>測(cè)試scrollWidth、clientWidth、offsetWidth</title>
<style type="text/css">
body, html {
margin: 0px;
padding: 0px;
}
#father {
width: 300px;
overflow: auto;
padding: 10px;
background: rebeccapurple;
border: 10px solid red;
margin: 20px;
}
#child {
height: 100px;
width: 1000px;
padding: 10px;
border: 20px solid #ffcc99;
margin: 30px;
}
</style>
</head>
<body>
<div id="father">
<div id="child"></div>
</div>
<script type="text/javascript">
var child = document.getElementById("child");
console.log("child.width:", window.getComputedStyle(child).width); //內(nèi)容的寬度:1000px
console.log("child.clientWidth:", child.clientWidth); //內(nèi)容+內(nèi)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 1020px
console.log("child.scrollWidth:", child.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1020px
console.log("child.offsetWidth:", child.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 1060px
var father = document.getElementById("father");
console.log("father.width:", window.getComputedStyle(father).width); //內(nèi)容的寬度:300px
console.log("father.clientWidth:", father.clientWidth); //內(nèi)容+內(nèi)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 320px
console.log("father.scrollWidth:", father.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1100px
console.log("father.offsetWidth:", father.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 340px
</script>
</body>
</html>
僅有橫向滾動(dòng)條的情況時(shí),父元素收受到子元素寬度的影響,其他比較特別的是scrollWidth。
父元素的scrollWidth是:子元素的content+padding+border+子元素一邊的margin+父元素一邊的padding。
2、有橫向滾動(dòng)條和豎向滾動(dòng)條的情況
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>測(cè)試scrollWidth、clientWidth、offsetWidth</title>
<style type="text/css">
body, html {
margin: 0px;
padding: 0px;
}
#father {
height: 50px;
width: 300px;
overflow: auto;
padding: 10px;
background: rebeccapurple;
border: 10px solid red;
margin: 20px;
}
#child {
height: 100px;
width: 1000px;
padding: 10px;
border: 20px solid #ffcc99;
margin: 30px;
}
</style>
</head>
<body>
<div id="father">
<div id="child"></div>
</div>
<script type="text/javascript">
var child = document.getElementById("child");
console.log("child.width:", window.getComputedStyle(child).width); //內(nèi)容的寬度:1000px
console.log("child.clientWidth:", child.clientWidth); //內(nèi)容+內(nèi)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 1020px
console.log("child.scrollWidth:", child.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1020px
console.log("child.offsetWidth:", child.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 1060px
var father = document.getElementById("father");
console.log("father.width:", window.getComputedStyle(father).width); //內(nèi)容的寬度:285px
console.log("father.clientWidth:", father.clientWidth); //內(nèi)容+內(nèi)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 305px
console.log("father.scrollWidth:", father.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1100px
console.log("father.offsetWidth:", father.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 340px
</script>
</body>
</html>
父元素的width為:父元素的content寬度-滾動(dòng)條的寬度(大約為15px)
父元素的clientWidth為:父元素的content寬度+父元素padding寬度-滾動(dòng)條寬度(大約為15px)
以上就是Element中clientWidth,offsetWidth,scrollWidth的區(qū)別的詳細(xì)內(nèi)容,更多關(guān)于clientWidth,offsetWidth,scrollWidth的區(qū)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript中三個(gè)等號(hào)和兩個(gè)等號(hào)你了解多少
本篇文章主要介紹了js里面的==和===,== 判斷如果兩邊變量的類型不同,而 === 則不做類型轉(zhuǎn)換,有興趣的可以了解一下2017-07-07
A標(biāo)簽中通過(guò)href和onclick傳遞的this對(duì)象實(shí)現(xiàn)思路
想傳遞當(dāng)前對(duì)象給一個(gè)函數(shù),于是就將這個(gè)URL寫(xiě)成"Javascript:shoControlSidebar(this)",可是結(jié)果發(fā)現(xiàn)這并不可行,接下來(lái)為大家詳細(xì)介紹下解決方法2013-04-04
javascript實(shí)現(xiàn)顏色漸變的方法
這篇文章介紹了javascript實(shí)現(xiàn)顏色漸變的方法,有需要的朋友可以參考一下2013-10-10
javascript和jquery實(shí)現(xiàn)設(shè)置和移除文本框默認(rèn)值效果代碼
這篇文章主要介紹了javascript和jquery實(shí)現(xiàn)設(shè)置和移除文本框默認(rèn)值效果代碼,本文實(shí)現(xiàn)的是類似html5 placeholder(空白提示)一種效果,需要的朋友可以參考下2015-01-01
微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)60s倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)60s倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
基于javascript實(shí)現(xiàn)圖片懶加載
這篇文章主要介紹了javascript實(shí)現(xiàn)圖片懶加載的方法及思路,有時(shí)我們需要用懶加載,也就是延遲加載圖片的方式,來(lái)提高網(wǎng)站的親和力,需要的朋友可以參考下2016-01-01
原生js實(shí)現(xiàn)打字動(dòng)畫(huà)游戲
本文主要分享了原生js實(shí)現(xiàn)打字動(dòng)畫(huà)游戲的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
JScript中使用ADODB.Stream判斷文件編碼的代碼
在實(shí)現(xiàn)TextStraem的時(shí)候,找到判斷文件編碼的代碼是VBS的,但是在JScript中是沒(méi)有ASC等函數(shù)的,也不能對(duì)二進(jìn)制數(shù)據(jù)進(jìn)行處理,因此需要通過(guò)一個(gè)特別的方法來(lái)獲取文件開(kāi)關(guān)的編碼標(biāo)識(shí)。2008-06-06
JS中注入eval, Function等系統(tǒng)函數(shù)截獲動(dòng)態(tài)代碼
這篇文章主要介紹了JS中注入eval, Function等系統(tǒng)函數(shù)截獲動(dòng)態(tài)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04

