当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序 页面滑动事件的实例详解

微信小程序 页面滑动事件的实例详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

微信小程序——页面滑动事件

wxml:

 <view id="id" class = "ball" bindtap = "handletap" bindtouchstart = "handletouchtart" bindtouchmove="handletouchmove" bindtouchend="handletouchend" style = "width : 100%px; height : 40px;">
 {{text}}
 </view>

wxss:

.ball {
  box-shadow:2px 2px 10px #aaa;
  border-radius: 20px;
  position: absolute; 
 }

js:

//js
page({
 data: {
  lastx: 0,     //滑动开始x轴位置
  lasty: 0,     //滑动开始y轴位置
  text: "没有滑动",
  currentgesture: 0, //标识手势
 },
 //滑动移动事件
 handletouchmove: function (event) {
  var currentx = event.touches[0].pagex
  var currenty = event.touches[0].pagey
  var tx = currentx - this.data.lastx
  var ty = currenty - this.data.lasty
  var text = ""
  //左右方向滑动
  if (math.abs(tx) > math.abs(ty)) {
   if (tx < 0)
    text = "向左滑动"
   else if (tx > 0)
    text = "向右滑动"
  }
  //上下方向滑动
  else {
   if (ty < 0)
    text = "向上滑动"
   else if (ty > 0)
    text = "向下滑动"
  }

  //将当前坐标进行保存以进行下一次计算
  this.data.lastx = currentx
  this.data.lasty = currenty
  this.setdata({
   text: text,
  });
 },

 //滑动开始事件
 handletouchtart: function (event) {
  this.data.lastx = event.touches[0].pagex
  this.data.lasty = event.touches[0].pagey
 },
 //滑动结束事件
 handletouchend: function (event) {
  this.data.currentgesture = 0;
  this.setdata({
   text: "没有滑动",
  });
 },
})

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网