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

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

JavaScript中clientWidth,offsetWidth,scrollWidth的區別

瀏覽:95日期: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
相關文章:
主站蜘蛛池模板: 欧美亚洲国产精品久久久久 | 日本aaaa片毛片免费观看 | 欧美人成在线观看ccc36 | 精品国产免费观看 | 久久久久久91香蕉国产 | 性欧美高清come | 久久精品国产400部免费看 | 国产黄色网 | 99久久综合精品国产 | 国产手机在线视频 | 免费人成在线观看网站视频 | 国产精品免费看久久久香蕉 | 伊人久久综合热青草 | 成人欧美精品一区二区不卡 | 国产精品1区2区 | 亚洲精品国产字幕久久不卡 | 精品视频网 | 在线成人精品国产区免费 | 精品国产日韩久久亚洲 | 伊大人香蕉久久网 | 久久午夜鲁丝片午夜精品 | 亚洲aⅴ男人的天堂在线观看 | 毛片免费观看视频 | 欧美成年黄网站色视频 | 波多野结衣手机视频一区 | 国产欧美一区视频在线观看 | 免费国产99久久久香蕉 | 欧美色操 | 欧美一区二区三区免费看 | 国产精品亚洲一区在线播放 | 欧美成人一区二区三区 | 天堂8在线天堂资源bt | 国产精品亚洲玖玖玖在线靠爱 | 国产一区二区在线观看视频 | aa日本| 亚洲欧美综合国产不卡 | 一级做a爰片久久毛片鸭王 一级做a爰全过程免费视频毛片 | 最近手机高清中文字幕大全7 | 国产成人综合亚洲欧美在 | 91精选视频在线观看 | 国产自线一二三四2021 |