box.offsetTop
时间: 2023-12-23 13:12:43 浏览: 24
The offsetTop property returns the distance of the top edge of an element relative to the top edge of the nearest positioned ancestor element or the document if there is no positioned ancestor. It is measured in pixels.
相关问题
touchmove(e) { console.log(this.$refs.dragToBox.$el,this.dom,'==',this.$refs.dragToBox.$el.offsetTop) if (this.canDrag && this.dom) { let picDom = this.$refs.dragPic.$el let picTop = picDom.offsetTop let picHeight = picDom.offsetHeight let picLeft = picDom.offsetLeft let picWidth = picDom.offsetWidth console.log('picDom:'+picDom,'picTop:'+picTop,'picHeight:'+picHeight,'picLeft:'+picLeft,'picWidth:'+picWidth) let boxDom = this.$refs.dragToBox.$el let boxHeight = boxDom.offsetHeight let boxWidth = boxDom.offsetWidth let boxTop = boxDom.offsetTop let boxLeft = boxDom.offsetLeft console.log('boxDom:'+boxDom,'boxHeight:'+boxHeight,'boxWidth:'+boxWidth,'boxTop:'+boxTop,'boxLeft:'+boxLeft) let to = this.dom let toH = to.offsetHeight let toW = to.offsetWidth let toPageH = e.touches[0].pageY - toH / 2 - boxTop let toPageW = e.touches[0].pageX - toW / 2 - boxLeft console.log('to:'+to,'toH:'+toH,'toW:'+toW,'toPageH:'+toPageH,'toPageW:'+toPageW) if(!this.el.enterPic && (picHeight + picTop - 38) > toPageH) { this.el.enterPic = true } if (!this.item.child[this.index + 1]) { this.item.child[this.index + 1] = { isMove: false, enterPic: false, left: 0, top: 0 } this.sqArr = Object.assign([], this.sqArr) } this.el.isMove = true if (toPageH >= 0 && ((this.el.enterPic && toPageH <= picHeight) || (!this.el.enterPic)) && toPageH <= window.innerHeight - toH) { this.el.top = `${((toPageH * 100) / boxHeight)}%` // this.el.top = toPageH + 'px' } if (toPageW >= picLeft && toPageW <= (picLeft + picWidth-toH) && toPageW <= window.innerWidth - toW) { // this.el.left = toPageW + 'px' this.el.left = `${(toPageW * 100) / boxWidth}%` } } },
这段代码是一个 `touchmove` 事件的处理函数。在移动触摸过程中,根据触摸点的位置和元素的尺寸,计算并更新元素的位置。
首先,获取了一些元素的引用和尺寸信息,比如 `picDom` 表示一个名为 `dragPic` 的元素,`boxDom` 表示一个名为 `dragToBox` 的元素。
然后,根据触摸点的位置和元素的尺寸,计算了一些值,如 `toPageH` 表示触摸点相对于某个元素的垂直位置,`toPageW` 表示触摸点相对于某个元素的水平位置。
根据这些计算出来的值,更新了元素的位置信息,如 `this.el.top` 和 `this.el.left` 分别表示元素的垂直和水平位置。
最后,根据一些条件判断,更新了一些状态变量,如 `this.el.isMove` 表示元素是否正在移动,`this.el.enterPic` 表示元素是否进入了特定条件下。
整个代码逻辑是基于触摸点位置和元素尺寸的计算和判断,实现了元素的拖拽效果。
offsetTop offsetLeft offsetBottom
`offsetTop`, `offsetLeft`, 和 `offsetBottom` 是在JavaScript中的CSS盒模型(Box Model)中用于获取元素相对于其最近的.offsetParent元素的位置的一些属性。这些属性通常用于定位和计算元素在页面或文档流中的偏移量。
1. `offsetTop`: 这个属性返回元素上边缘到其offsetParent元素上边缘的距离,包括padding和border但不包括margin。
2. `offsetLeft`: 类似于`offsetTop`,但它返回元素左边缘到其offsetParent元素左边缘的距离。
3. `offsetBottom`: 返回元素下边缘到其offsetParent元素下边缘的距离,同样考虑了padding和border但不包括margin。
这些属性常用于动态计算元素位置、滚动条处理或者创建绝对定位的布局。如果你想要计算元素相对于视口的位置,可能还需要加上元素自身的宽度、高度和`scrollLeft`、`scrollTop`等值。
相关推荐
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)