JS拿到页面某个元素的坐标范围

文章讲述了如何通过JavaScript监听鼠标的拖拽事件,获取拖动元素和目标table行的坐标,判断拖动元素是否在特定table行的范围内,从而执行相应的业务逻辑。代码示例展示了如何遍历table行并计算坐标来确定目标行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求多出现在需要拖拽的事件中,监听鼠标的坐标和目标坐标范围对比,执行业务逻辑,这个情况比较自由。

1.直接代码实现吧

我的需求是:获取的是拖动table某一行到table中的另一行,确定目标行的数据(这个后面发现onDrop方法里面也能拿到)


const table_dom = document.getElementsByTagName('tbody');// 获得要操作的Table的Dom
const trdom = table_dom[0].getElementsByTagName('tr');// tr元素的Dom,因为不止一行,所以是数组
const dropPoint = e.target.getBoundingClientRect(); // 这里是拿到拖动然后放置后的鼠标坐标点
for (let i = 0; i < trdom?.length; i++) {
    // 这里循环就是找到每一行对于浏览器的坐标,然后根据上下左右进行计算,判断是否在范围内即可;
    const currentDom = trdom[i].getBoundingClientRect();
    if (dropPoint.left >= currentDom.left && dropPoint.right <= currentDom.right &&             
    dropPoint.top >= currentDom.top && dropPoint.bottom <= currentDom.bottom) {
    // 这样我们就找出了tr的某一行,然后执行业务逻辑
    ......
    break;
  }
}

 上述代码是:js原生获取某个元素相对于浏览器来说的坐标范围。

### UniApp 滑块组件获取中心点坐标的方法 在 UniApp 的 `swiper` 组件中,可以通过监听特定事件并结合一些计算来获得当前显示页面的中心点位置。具体来说: 对于滑块视图(`swiper`),可以利用其提供的属性和事件来进行处理。为了得到滑块组件的中心点坐标,通常的做法是在每次切换完成时触发相应的回调函数,并在此处执行必要的逻辑。 当需要获取某个子项的具体位置信息时,可以借助于 JavaScript 提供的一些 DOM API 来实现这一目标。例如,通过调用 `getBoundingClientRect()` 方法能够方便地取得元素相对于视口的位置数据[^3]。 下面是一个简单的例子展示如何在一个页面加载完成后以及每一页切换之后更新并打印出当前页码对应图片的中心点坐标: ```javascript // pages/index/index.vue <template> <view class="container"> <!-- Swiper Component --> <swiper @change="onSwiperChange" :current="currentIndex"> <swiper-item v-for="(item, index) in items" :key="index"> <image :src="item.src"></image> </swiper-item> </swiper> <!-- Display Center Point Coordinates --> <text>{{ centerPoint }}</text> </view> </template> <script> export default { data() { return { currentIndex: 0, items: [ { src: 'path/to/image1.jpg' }, { src: 'path/to/image2.jpg' } ], centerPoint: '' }; }, methods: { onSwiperChange(event) { const newIndex = event.detail.current; this.currentIndex = newIndex; // Update the current item's image element after transition ends. setTimeout(() => { this.updateCenterPoint(); }, 300); // Adjust delay as needed based on your swiper animation duration }, updateCenterPoint() { let query = uni.createSelectorQuery().in(this); query.select('.swiper-item').boundingClientRect((rect) => { if (!rect) return; // Calculate and set the new center point coordinates this.centerPoint = `X: ${rect.left + rect.width / 2}, Y: ${rect.top + rect.height / 2}`; }).exec(); } }, onLoad() { // Initialize with first slide's center point when page loads this.$nextTick(() => { this.updateCenterPoint(); }); } }; </script> <style scoped> /* Add styles here */ .swiper-item img { width: 100%; } .container text { position: absolute; bottom: 20px; color: white; font-size: large; } </style> ``` 在这个案例里,每当用户改变幻灯片的时候都会重新计算新的中心点坐标,并将其显示出来。这里使用了 `setTimeout` 函数等待动画结束再查询元素尺寸,确保能拿到正确的布局信息;同时也考虑到了首次渲染的情况,在页面加载完毕后立即初始化一次中心点坐标
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值