亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

JavaScript中clientWidth,offsetWidth,scrollWidth的區別

瀏覽:90日期:2023-10-05 10:51:31
一、概念

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

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

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

<!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、有橫向滾動條和豎向滾動條的情況

<!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的區別的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 一集毛片 | 韩日视频在线观看 | 妖精视频在线观看18 | 国产在线观看一区二区三区 | 中国一级淫片aaa毛片毛片 | 免费免费啪视频在线 | 亚洲狠狠ady亚洲精品大秀 | 在线看国产| 逼逼毛片 | 日韩在线www | 国产午夜亚洲精品一区网站 | 久久这里只有精品免费看青草 | 玖玖玖精品视频免费播放 | 欧美视频在线观看网站 | 免费观看国产精品 | 天天干天天色综合网 | 一级特大黄色片 | 国内自拍亚洲 | 国产精品欧美亚洲区 | 欧美一级三级在线观看 | 国产视频在线观看福利 | 97色涩| 91精品国产91久久 | 黑人一区 | 久久亚洲国产的中文 | 国产成人精品视频免费大全 | 国产精品一区二区免费福利视频 | 成人做爰网站 | 日韩一级大毛片欧美一级 | 国产亚洲精品自在久久不卡 | 日本一级特黄在线播放 | 成人国产mv免费视频 | 91视频91| 色视频免费国产观看 | 国产高清在线精品一区αpp | 精品无码久久久久久国产 | 久久久久国产一级毛片高清片 | 国产大尺度吃奶无遮无挡 | 国产美女精品久久久久中文 | 欧美黑人双插 | 东京加勒比中文字幕波多野结衣 |