av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

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

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

二、舉例1、僅有橫向滾動(dòng)條的情況

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>測(cè)試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)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 1020px console.log('child.scrollWidth:', child.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1020px console.log('child.offsetWidth:', child.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 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)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 320px console.log('father.scrollWidth:', father.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1100px console.log('father.offsetWidth:', father.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 340px</script></body></html>

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

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

2、有橫向滾動(dòng)條和豎向滾動(dòng)條的情況

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>測(cè)試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)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 1020px console.log('child.scrollWidth:', child.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1020px console.log('child.offsetWidth:', child.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 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)邊距-滾動(dòng)條-----不包括邊框和外邊距 == 可視內(nèi)容 305px console.log('father.scrollWidth:', father.scrollWidth); //內(nèi)容+內(nèi)邊距+溢出尺寸-----不包括邊框和外邊距 ==實(shí)際內(nèi)容 1100px console.log('father.offsetWidth:', father.offsetWidth); //元素的寬度(內(nèi)容+內(nèi)邊距+邊框+滾動(dòng)條)==整體,整個(gè)控件 340px</script></body></html>

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

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

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

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 在线一区视频 | 三年中文在线观看免费大全中国 | 亚洲精品成人 | 久久艳片www.17c.com | 天天插天天爽 | 九九热视频在线观看 | 久久综合爱 | 日韩一级免费视频 | 久久精品久久久 | 91成人亚洲 | 日本加勒比在线观看 | 国产精品久久一区二区三区 | 中文天堂在线观看 | 国产精彩视频 | 一区二区三区在线免费观看 | 肉丝美脚视频一区二区 | 人人爽人人爽人人爽 | 成人免费观看网站 | 九色av | 精品国产999久久久免费 | a一级黄色片 | 我要看一级黄色片 | 免费看黄色录像 | 亚洲一区精品视频 | 伊人成人在线视频 | 国产一区二区中文字幕 | 日本一级淫片 | 青青草伊人网 | 黄色在线观看网址 | 国产精品人人做人人爽人人添 | 国产毛片一级 | 日韩视频二区 | 在线看91 | 日韩三级久久 | 日韩久久av| 亚洲三级视频 | 一区二区三区精品 | 人人爽爽人人 | 欧美激情自拍 | 中文字幕在线观看免费 | 亚洲免费专区 |