生活随笔
收集整理的這篇文章主要介紹了
HTML中各种 div 位置距离关系
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HTML中各種 div 位置距離關系
- 一. 盒模型圖片展示:
- 二. 位置距離計算屬性
- 三. 應用場景
一. 盒模型圖片展示:
二. 位置距離計算屬性
offsetWidth, offsetHeight
獲取盒子的寬度/高度(包括盒子的border,padding和內容width/height),不包括外邊距
offsetLeft
獲取盒子當前位置(左上角)距離自己最近定位的父元素左側的距離,如果沒有最近的定位的父元素,則相當于HTML
offsetTop
獲取盒子當前位置距離自己最近定位的父元素頂部的距離,如果沒有最近的定位的父元素,則相當于HTML
clientWidth,clientHeight
獲取盒子的可視區域寬度、高度,包括padding在內,不包括border,這里是不包括滾動條的情況,如果有滾動條,發現盒子的寬度(width)從100變成83,可知滾動條的寬度為17px,而且是占據盒子內容的寬度,除了可視寬高,似乎其他都沒有影響。
所謂的可視區域,個人理解就是一個盒子里面,能夠展示出被人看見的內容區域
clientLeft
獲取盒子的左邊框的寬度,可理解為可視區域和左側邊(這個左側邊不是border)之間的距離
clientTop
獲取盒子的上邊框的寬度,可理解為可視區域和上側邊(這個上側邊不是border)之間的距離
scrollWidth,scrollHeight
獲取盒子內容里面元素占據的真正寬度、高度
scrollLeft,scrollTop
滾動條距離左側邊,上側邊的距離
滾動條的最大滾動高度為滾動高度(scrollHeight) - 盒子可視高度(clientHeight)
innerWidth,innerHeight
窗口寬度、高度,也可以理解為window窗口的可視區域寬度、高度
let IH = window
.innerHeight
if (document
.documentElement
) {let IH = document
.documentElement
.clientHeight
} else {let IH = document
.body
.clientHeight
}
clientX & clientY
鼠標點擊或者觸屏時,點擊位置距離window可視區域左上角的(0, 0)的坐標距離
<div @touchstart
="touchStart($event)" @touchmove
="touchMove($event)" @touchend
="touchEnd($event)"></div
>touchStart(e
){let info
= this.dropDownInfo
;info
.startX
= e
.targetTouches
[0].clientX
;info
.startY
= e
.targetTouches
[0].clientY
;console
.log(e
)
},
pageX & pageY
正常情況下,window的可視區域的是不變的,所以相對于可視區域的坐標也就不會變,無論怎么點,clientX和clientY都是一樣的。但是從page就可以知道,這個是頁面坐標,也就是點擊的頁面距離window可視區域左上角的坐標距離。
一般可視區域是固定的,但是頁面大小就不一定是固定的,如果有滾動條,說面頁面大小超過了可視區域,這時候點擊滾動隱藏區域,pageX&pageY肯定是大于clientX&clientY
screenX & screenY
點擊的位置距離屏幕的左上角的上左距離
三. 應用場景
swiper左右滑動切換列表的內部嵌套slide滑塊
問題:內部slide滑塊滑動事件與swiper左右滑動切換事件沖突,影響性能
解決辦法:監聽滑塊滑到觸左、觸右,觸左時可觸發swiper左切換,觸右時可觸發swiper右切換,除此之外,內部滑動阻止冒泡
<div @scroll="scrollEvent" @touchstart="touchStart($event)" @touchmove="touchMove($event)" class="slide"><div class="slide__item"></div><div class="slide__item"></div><div class="slide__item"></div>
</div>
data() {return {dropDownInfo
: {startX
: 0,startY
: 0,isDropDown
: false, isBorder
: false,},leftCanChange
: true, rightCanChange
: false, }
},
methods
: {scrollEvent(e
){const slideWidth
= e
.target
.scrollWidth
; const offsetWidth
= e
.target
.offsetWidth
; const scrollLeft
= e
.target
.scrollLeft
; if (scrollLeft
<= 0) {this.leftCanChange
= true} else if (scrollLeft
+ offsetWidth
>= slideWidth
) {this.rightCanChange
= true} else {this.leftCanChange
= falsethis.rightCanChange
= false}},touchStart(e
){const info
= this.dropDownInfo
;info
.startX
= e
.targetTouches
[0].pageX
;info
.startY
= e
.targetTouches
[0].pageY
;},touchMove(e
){let info
= this.dropDownInfo
;const X = e
.targetTouches
[0].pageX
- info
.startX
;const disX
= Math
.abs(X)const disY
= e
.targetTouches
[0].pageY
- info
.startY
if ((disX
> disY
) && this.options
.length
> 1) {if (!(this.leftCanChange
&& X > 0 || this.rightCanChange
&& X < 0)) {e
.stopPropagation();}}},
}
.slide{position: relative
;overflow: hidden
;overflow-x: auto
;white-space: nowrap
;-webkit-overflow-scrolling: touch
;background: #f5f5f5
;
}
.slide__item{display: inline-block
;width: 6.58rem
;min-height: 3rem
;margin: 0 0.08rem
;vertical-align: top
;background: #ffffff
;
}
2. 監聽元素是否在可視區
mounted() {window
.addEventListener("scroll", this.handleScroll
, false);
},
methods
: {handleScroll(e
) {let el
= document
.getElementById("elementId"); let isVideoVisible
= this.$util
.isElementVisible(el
); }
},
util
.js
function isElementVisible(el
) {const rect
= el
.getBoundingClientRect();const vWidth
= window
.innerWidth
|| document
.documentElement
.clientWidth
;const vHeight
= window
.innerHeight
|| document
.documentElement
.clientHeight
;const efp = function (x
, y
) {return document
.elementFromPoint(x
, y
);};if (rect
.right
< 0 || rect
.bottom
< 0|| rect
.left
> vWidth
|| rect
.top
> vHeight
) return false;return (el
.contains(efp(rect
.left
, rect
.top
))|| el
.contains(efp(rect
.right
, rect
.top
))|| el
.contains(efp(rect
.right
, rect
.bottom
))|| el
.contains(efp(rect
.left
, rect
.bottom
)));
}
以上代碼只對上下滾動監聽有用
在一個頁面的slide左右滾動滑塊中監聽的話要用別的方法
**getBoundingClientRect();**獲取元素距離瀏覽器周邊的位置的方法
<div @scroll="scrollEvent" class="council__slide"><divv-for="(item, index) in options":key="'list-'+ index"class="council__slide-item"></div>
</div>
.council__slide-box{position: relative
;overflow: hidden
;overflow-x: auto
;white-space: nowrap
;-webkit-overflow-scrolling: touch
;&::-webkit-scrollbar {width: 0
;height: 0
;display: none
;background: transparent
;}
}
.council__slide-item{display: inline-block
;width: 6.78rem
;height: 3.86rem
;border-radius: 0.4rem
;overflow: hidden
;margin: 0 0.1rem
;
}
created () {this.throttleScroll
= $util
.throttle(this.pageScroll
, 100);
},
mounted () {},
methods
: {scrollEvent(){this.throttleScroll();},pageScroll () {let el
= document
.getElementById('playVideo');const rectLeft
= el
.getBoundingClientRect(); if (Math
.abs(rectLeft
.left
) >= 187) {...}}},
}
<input type
="text" id
="inp" /><script type
="text/javascript">
var box
= document
.getElementById( "inp" );
alert(box
.getBoundingClientRect().top
);
alert(box
.getBoundingClientRect().right
);
alert(box
.getBoundingClientRect().bottom
);
alert(box
.getBoundingClientRect().left
);
function getRect( elements
){ var rect
= elements
.getBoundingClientRect(); var clientTop
= document
.documentElement
.clientTop
; var clientLeft
= document
.documentElement
.clientLeft
; return { top
: rect
.top
- clientTop
, bottom
: rect
.bottom
- clientTop
, left
: rect
.left
- clientLeft
, right
: rect
.right
- clientLeft
};
};
</script
>
如何判斷元素是否進入可視區域viewport?
總結
以上是生活随笔為你收集整理的HTML中各种 div 位置距离关系的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。