微信小程序: mpvue touchend获取元素位置相当于 onmousedown

浏览器有鼠标按下事件

document.onmousedown 事件是依赖于DOM ,微信小程序有相似的事件。就是bind:touchend
下面这里是mpvue用法

<!--原生 <view bind:touchend="touchEnd()">按下</view>-->
<div class="item_info" @touchend="touchEnd"><!--touchstart相当于onmousedown-->
  <div  v-for="(item, i) in List" :key="i" wx:key="i">
     <item :item="item">这里显示每一个元素模块</item>
   </div>
 </div>

这里使用query.select('#the-id').boundingClientRect() 可以获取元素距屏幕左侧的距离

touchEnd() {
    let _that = this;
    const query = wx.createSelectorQuery();
    query.select(".item_info").boundingClientRect();
    query.exec(function(res) {
     	console.log(res[0].left)//获取元素距左侧距离
    });
  };
发布了156 篇原创文章 · 获赞 531 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_39043923/article/details/103731830