国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

JavaScript中clientWidth,offsetWidth,scrollWidth的區別

瀏覽:89日期: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
相關文章:
主站蜘蛛池模板: 亚洲人成影院午夜网站 | www.日本三级 | 国产成人综合网亚洲欧美在线 | 一区二区三区四区在线免费观看 | 国产精品视频久久久久久 | 大陆孕妇孕交视频自拍 | 国产成人在线小视频 | 国产片自拍 | 性生活免费视频网站 | 欧美成人私人视频88在线观看 | 91丨九色丨首页在线观看 | 欧美一级纶理片免费 | 日本午夜人成免费视频 | 久久久亚洲精品国产 | 久久精品女人毛片国产 | 欧美整片在线观看 | 国产三级a | 日本aa毛片a级毛片免费观看 | 办公室紧身裙丝袜av在线 | 国产亚洲欧美视频 | 亚洲视频在线观 | 美女被强行扒开双腿激情视频 | 国产成人a毛片在线 | 99久久免费精品国产免费高清 | 国产免费专区 | 精品国产高清在线看国产 | 色噜噜国产精品视频一区二区 | 7m视频精品凹凸在线播放 | 亚洲区精品久久一区二区三区 | 午夜三级理论在线观看视频 | 国产高清视频免费 | 久草在线国产 | 三级精品| 国产精品亚洲精品久久成人 | 成年人免费在线视频观看 | 国产一区在线看 | 亚洲一区高清 | 五月激情丁香婷婷综合第九 | 国产综合久久 | 夜夜操夜夜爽 | 美女张开腿给男生桶下面视频 |