色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

JavaScript中clientWidth,offsetWidth,scrollWidth的區別

瀏覽:96日期: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
相關文章:
主站蜘蛛池模板: 久草视频在线播放 | 欧美视频一区 | 九九九国产视频 | 一区二区国产在线播放 | 精品久久久久久免费影院 | 在线a亚洲视频播放在线观看 | 日韩美女一区二区三区 | 午夜精品久久久久久91 | 欧美观看一级毛片 | 成人欧美一区二区三区在线 | 成年人福利视频 | 久久精品亚洲一区二区 | 不卡精品国产_亚洲人成在线 | 亚洲精品亚洲人成人网 | 性感美女一级毛片 | 怡红院成人网 | 国产在线欧美日韩一区二区 | 九九视频在线观看视频6偷拍 | 日韩麻豆 | 国产美女在线一区二区三区 | 青青青青爽视频在线播放 | 大陆精品自在线拍国语 | 男人天堂网在线观看 | 97在线观看 | 99热国产免费 | 久久精品全国免费观看国产 | 国产亚洲精品久久久久久久 | 国产高清毛片 | a级国产乱理伦片在线观看99 | 精品国产一区二区三区在线 | 最新三级网站 | 欧美日本一区亚洲欧美一区 | 久草视频国产 | 国产区精品一区二区不卡中文 | 免费一级毛片私人影院a行 免费一级毛片无毒不卡 | 久香草视频在线观看 | 一品道一本香蕉视频 | 精品72久久久久久久中文字幕 | 亚洲欧美久久精品1区2区 | 亚洲精品区在线播放一区二区 | 久久久久国产成人精品亚洲午夜 |