日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight是幾個困惑了好久的元素屬性,趁著有時間整理一下

1. clientWidth 和 clientHeight?

網頁中的每個元素都具有?clientWidth 和 clientHeight 屬性,表示可視區域的寬高,即元素內容加上padding以后的大小,而不包括border值和滾動條的大小,

如下圖所示:

示例代碼如下:

HTML代碼:

<div id="demo"><p></p> </div>

CSS代碼:

div{width: 100px;height: 100px;overflow: scroll;border:20px solid #7b7676;padding: 30px;margin: 60px; } p{width: 180px;height: 160px;background: #aac7aa; }

如下圖:

從上圖得出:

clientWidth = 內容 + padding 即 83+30*2 = 143

clientHeight =?內容 + padding 即 83+30*2 = 143

2.?scrollWidth 、scrollHeight 、scrollLeft、scrollTop

scrollWidth 和 scrollHeight 表示內容的完整寬高,包括padding以及沒有完全顯示出來的部分,不包括滾動條

scrollWidth = padding+包含內容的完全寬度

scrollHeight =?padding+包含內容的完全高度

scrollLeft和scrollTop都表示滾動條滾動的部分

如下圖:

依然利用上面的例子:

scrollWidth:160 + 30 = 190

scrollHeight:180 + 30 = 210

至于為什么padding只加30而不是30*2呢,如上圖所示,超出隱藏部分距離父元素邊框是沒有padding的,所以只加一個

3. offsetWidth ?和 offsetHeight

? ?如下圖所示:

offsetWidth表示元素本身的寬度,包括滾動條和padding,border

offsetHeight表示元素本身的高度,包括滾動條和padding,border

所以例子中的offsetWidth = 100 + 20 * 2 + 30 * 2 = 200

? ? ? ? ? ? ? ? ? ? ? ? ? ? offsetHeight = 100 + 20 * 2 + 30 *2 = 200

offsetTop 和 offsetLeft 表示該元素的左上角與父容器(offsetParent對象)左上角的距離

參考鏈接:http://www.ruanyifeng.com/blog/2009/09/find_element_s_position_using_javascript.html

by新手小白的記錄

?

轉載于:https://www.cnblogs.com/lynnmn/p/6383246.html

總結

以上是生活随笔為你收集整理的clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。