在实际的移动应用程序交互方式中,最常见的就是滑动操作。像左右滑动切换页面,手指张开来放大图片等,都是由滑动操作来完成的。
微信小程序默认提供的相关事件如下:
tap对应点击操作,还提供了longtap来支持长按操作,这些都比较简单,就不多做讲述。
touchmove对应滑动操作,通过bindtouchmove即可响应滑动操作。
//wxml
<view id="id" bindtouchmove="handletouchmove" style = "width : 100px; height : 100px; background : #167567;">
</view>
//js
Page({
handletouchmove: function(event) {
console.log(event)
},
})
当按住view标签并滑动鼠标时,会不停的触发滑动事件,直到放开鼠标,当鼠标移出view标签区域后依然会触发事件。
拖拽操作
通过监听滑动事件,可以实现一些实用的功能,比如用过iphone的用户都知道assistivetouch,一个桌面上的快捷按钮,可以将按钮拖动到桌面的任意位置。为了方便,这里就用一个圆形来代表该按钮。
//wxml
<view id="id" class = "ball" bindtouchmove="handletouchmove" style = "width : 60px; height : 60px; background : #545345;">
</view>
//wxss
.ball {
box-shadow:2px 2px 10px #AAA;
border-radius: 20px;
position: absolute;
}
//js
Page({
handletouchmove: function(event) {
console.log(event)
},
})
好吧,按钮丑了点,这不是重点。拖拽操作的实现思路也很简单,当触发滑动事件时,event对象会包含当前触摸位置的坐标信息,可以通过event.touches[0].pageX和event.touches[0].pageY 来获取,为什么touches是数组呢,答案是为了支持多点触控(在电脑上不知道如何模拟多点触控)。接下来将按钮的位置设置为触摸位置,应该就能实现预期效果了,让我们试试看。
在Page中定义按钮的位置数据ballBottom和ballRight,并绑定到view的对应属性中。
//wxml
<view id="id" class = "ball" bindtouchmove="handletouchmove" style = "width : 60px; height : 60px; background : #545345; top:{
{ballTop}}px; left: {

最低0.47元/天 解锁文章
1035





