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

您的位置:首頁技術(shù)文章
文章詳情頁

JavaScript中clientWidth,offsetWidth,scrollWidth的區(qū)別

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

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

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

二、舉例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); //內(nèi)容的寬度:1000px console.log('child.clientWidth:', child.clientWidth); //內(nèi)容+內(nèi)邊距-滾動條-----不包括邊框和外邊距 == 可視內(nèi)容 1020px console.log('child.scrollWidth:', child.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內(nèi)容 1020px console.log('child.offsetWidth:', child.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動條)==整體,整個控件 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)邊距-滾動條-----不包括邊框和外邊距 == 可視內(nèi)容 320px console.log('father.scrollWidth:', father.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內(nèi)容 1100px console.log('father.offsetWidth:', father.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動條)==整體,整個控件 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); //內(nèi)容的寬度:1000px console.log('child.clientWidth:', child.clientWidth); //內(nèi)容+內(nèi)邊距-滾動條-----不包括邊框和外邊距 == 可視內(nèi)容 1020px console.log('child.scrollWidth:', child.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內(nèi)容 1020px console.log('child.offsetWidth:', child.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動條)==整體,整個控件 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)邊距-滾動條-----不包括邊框和外邊距 == 可視內(nèi)容 305px console.log('father.scrollWidth:', father.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實際內(nèi)容 1100px console.log('father.offsetWidth:', father.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動條)==整體,整個控件 340px</script></body></html>

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

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

以上就是Element中clientWidth,offsetWidth,scrollWidth的區(qū)別的詳細內(nèi)容,更多關(guān)于clientWidth,offsetWidth,scrollWidth的區(qū)別的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 天堂av2017男人的天堂 | 美女曰皮| xxxwww欧美| 窝窝午夜看片七次郎青草视频 | 国产欧美日韩精品一区二区三区 | 欧美在线视频免费观看 | 91久久国产露脸精品 | 国产成人久久久精品毛片 | 亚洲一区二区三区不卡视频 | 国产日韩一区二区三区在线观看 | 精品一区二区三区在线成人 | 国产精品久久不卡日韩美女 | 欧美日韩精品在线视频 | 国产午夜精品久久理论片 | 欧美视频精品在线观看 | 亚洲一级特黄特黄的大片 | 欧美性巨大欧美 | 毛片直接看 | 美日韩黄色片 | 国产91久久精品一区二区 | 久久99久久99精品免观看 | 亚洲精品区在线播放一区二区 | 国产一区二区三区视频在线观看 | 性欧美美国级毛片 | 亚洲免费一级视频 | 国产精品1区2区3区在线播放 | 99在线免费观看视频 | 伊人久久免费 | 欧美视频一区二区三区精品 | 久久亚洲国产成人亚 | 国产伦久视频免费观看 视频 | 日韩精品一区二三区中文 | 亚洲一区二区三区首页 | 免费看黄色片的网站 | 亚洲视频区 | 精品久久在线 | 欧美上床视频 | 中国老太卖淫播放毛片 | 无码免费一区二区三区免费播放 | 国产精品黄在线观看免费 | 高清毛片免费看 |