国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

香港云服务器
服務器之家 - 編程語言 - JavaScript - js教程 - JavaScript中clientWidth,offsetWidth,scrollWidth的區別

JavaScript中clientWidth,offsetWidth,scrollWidth的區別

2022-01-06 15:25guo&qi js教程

這篇文章主要介紹了Element中clientWidth,offsetWidth,scrollWidth的區別,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下

一、概念

  它們都是Element的屬性,表示元素的寬度:

Element.clientWidth    內容+內邊距-滾動條-----不包括邊框和外邊距  == 可視內容
Element.scrollWidth    內容+內邊距+溢出尺寸-----不包括邊框和外邊距  ==實際內容
Element.offsetWidth   元素的寬度(內容+內邊距+邊框+滾動條)==整體,整個控件

二、舉例

1、僅有橫向滾動條的情況

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>測試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); //內容的寬度:1000px
 console.log("child.clientWidth:", child.clientWidth); //內容+內邊距-滾動條-----不包括邊框和外邊距 == 可視內容 1020px
 console.log("child.scrollWidth:", child.scrollWidth); //內容+內邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內容 1020px
 console.log("child.offsetWidth:", child.offsetWidth); //元素的寬度(內容+內邊距+邊框+滾動條)==整體,整個控件  1060px
 
 var father = document.getElementById("father");
 console.log("father.width:", window.getComputedStyle(father).width); //內容的寬度:300px
 console.log("father.clientWidth:", father.clientWidth); //內容+內邊距-滾動條-----不包括邊框和外邊距 == 可視內容 320px
 console.log("father.scrollWidth:", father.scrollWidth); //內容+內邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內容 1100px
 console.log("father.offsetWidth:", father.offsetWidth); //元素的寬度(內容+內邊距+邊框+滾動條)==整體,整個控件  340px
</script>
</body>
</html>

  僅有橫向滾動條的情況時,父元素收受到子元素寬度的影響,其他比較特別的是scrollWidth。

  父元素的scrollWidth是:子元素的content+padding+border+子元素一邊的margin+父元素一邊的padding。

2、有橫向滾動條和豎向滾動條的情況

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>測試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); //內容的寬度:1000px
 console.log("child.clientWidth:", child.clientWidth); //內容+內邊距-滾動條-----不包括邊框和外邊距 == 可視內容 1020px
 console.log("child.scrollWidth:", child.scrollWidth); //內容+內邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內容 1020px
 console.log("child.offsetWidth:", child.offsetWidth); //元素的寬度(內容+內邊距+邊框+滾動條)==整體,整個控件  1060px
 
 var father = document.getElementById("father");
 console.log("father.width:", window.getComputedStyle(father).width); //內容的寬度:285px
 console.log("father.clientWidth:", father.clientWidth); //內容+內邊距-滾動條-----不包括邊框和外邊距 == 可視內容 305px
 console.log("father.scrollWidth:", father.scrollWidth); //內容+內邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內容 1100px
 console.log("father.offsetWidth:", father.offsetWidth); //元素的寬度(內容+內邊距+邊框+滾動條)==整體,整個控件  340px
</script>
</body>
</html>

  父元素的width為:父元素的content寬度-滾動條的寬度(大約為15px)

  父元素的clientWidth為:父元素的content寬度+父元素padding寬度-滾動條寬度(大約為15px)

以上就是Element中clientWidth,offsetWidth,scrollWidth的區別的詳細內容,更多關于clientWidth,offsetWidth,scrollWidth的區別的資料請關注服務器之家其它相關文章!

原文鏈接:https://www.cnblogs.com/gg-qq/p/14309110.html

延伸 · 閱讀

精彩推薦
  • js教程基于JavaScript實現輪播圖效果

    基于JavaScript實現輪播圖效果

    這篇文章主要為大家詳細介紹了基于JavaScript實現輪播圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    努力學習中.....5542021-12-24
  • js教程js實現電燈開關效果

    js實現電燈開關效果

    這篇文章主要為大家詳細介紹了js實現電燈開關效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    小蘇(o﹃o )7092022-01-04
  • js教程微信小程序視頻彈幕發送功能的實現

    微信小程序視頻彈幕發送功能的實現

    這篇文章主要介紹了微信小程序視頻彈幕發送功能的實現,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的...

    保護我方豆豆4862021-12-21
  • js教程three.js中多線程的使用及性能測試詳解

    three.js中多線程的使用及性能測試詳解

    這篇文章主要給大家介紹了關于three.js中多線程的使用及性能測試的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考...

    郭先生的博客4472021-12-28
  • js教程JavaScript this關鍵字的深入詳解

    JavaScript this關鍵字的深入詳解

    這篇文章主要給大家介紹了關于JavaScript this關鍵字的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要...

    JAVA_樸先生9062021-12-31
  • js教程原生JavaScript實現留言板

    原生JavaScript實現留言板

    這篇文章主要為大家詳細介紹了原生JavaScript實現留言板,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    棟棟很優秀啊5292021-12-29
  • js教程js實現隨機點名

    js實現隨機點名

    這篇文章主要為大家詳細介紹了js實現隨機點名,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    搬磚大法10062022-01-04
  • js教程three.js顯示中文字體與tween應用詳析

    three.js顯示中文字體與tween應用詳析

    這篇文章主要給大家介紹了關于three.js顯示中文字體與tween應用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習...

    郭志強9802021-12-24
869
主站蜘蛛池模板: 成人一区二区在线 | 四虎影院在线免费播放 | 国产成人精品久久二区二区 | 亚洲精品粉嫩美女一区 | 九色在线 | 亚洲专区 变态 另类 | 亚洲精品乱码久久久久膏 | 亚洲午夜精品久久久久久高潮 | 国产精品亚洲第一区在线暖暖韩国 | 婷婷激情综合 | 久久这里有精品视频 | 国产一区二区三区高清 | 亚洲国产精品电影在线观看 | 午夜电影网站 | 五月婷婷丁香 | 成人一区二区在线 | 欧美日本韩国在线 | 色网站在线免费观看 | 激情欧美一区二区三区中文字幕 | 国产精品久久久久久久久 | 亚洲成人精品在线 | 亚洲激情视频在线 | 欧美日韩一区二区在线观看 | 国产精品精品视频 | 精品免费| 最新中文字幕视频 | 免费h| av电影免费在线看 | 在线播放国产一区二区三区 | 亚洲三级视频 | 毛片免费在线播放 | 欧美黑人性暴力猛交喷水黑人巨大 | 亚洲第一视频网站 | 亚洲人成网站999久久久综合 | 黄视频在线观看免费 | 色婷婷蜜桃| 国产一级片 | 成人在线免费观看视频 | 欧美一区二区久久 | 中文字幕精品视频 | 国产精品网站在线观看 |