怎么在微信小程序中实现一个手势滑动卡片效果

发布时间:2021-04-19 17:47:36 作者:Leah
来源:亿速云 阅读:418

怎么在微信小程序中实现一个手势滑动卡片效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

代码:

//设置position: absolute; left:50%;后,再 margin-left:负(一半的width);可以实现水平居中
//同理,设置top:50%;margin-top:负(一半height);可以实现垂直居中
.body-swiper {
 width: 680rpx;//rpx是为了适应屏幕
 height: 900rpx;
 left: 50%;
 position: absolute;
 margin-left: -340rpx;
 z-index: 3;
 box-sizing: border-box;
 -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 60px rgba(0, 0, 0, 0.06) inset;
 -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 40px rgba(0, 0, 0, 0.06) inset;
 box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 40px rgba(0, 0, 0, 0.06) inset;
 border-radius: 12px;
}
 
.body-swiper2 {
 width: 640rpx;
 height: 900rpx;
 left: 50%;
 position: absolute;
 margin-left: -320rpx;
 z-index: 2;
 box-sizing: border-box;
 -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 60px rgba(0, 0, 0, 0.06) inset;
 -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 40px rgba(0, 0, 0, 0.06) inset;
 box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 40px rgba(0, 0, 0, 0.06) inset;
 border-radius: 12px;
}
 
.body-swiper3 {
 width: 605rpx;
 height: 900rpx;
 left: 50%;
 position: absolute;
 margin-left: -302.5rpx;
 z-index: 1;
 box-sizing: border-box;
 -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 60px rgba(0, 0, 0, 0.06) inset;
 -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 40px rgba(0, 0, 0, 0.06) inset;
 box-shadow: 0 1px 4px rgba(0, 0, 0, 0.62), 0 0 40px rgba(0, 0, 0, 0.06) inset;
 border-radius: 12px;
}

接下来,是卡片手势的实现;

上代码:

/**
 * 卡片1手势
 */
 touchstart1: function (event) {
 touchDotX = event.touches[0].pageX; // 获取触摸时的原点
 touchDotY = event.touches[0].pageY;
 console.log("起始点的坐标X:" + touchDotX);
 console.log("起始点的坐标Y:" + touchDotY);
 },
 // 移动结束处理动画
 touchend1: function (event) {
 // 手指离开屏幕时记录的坐标
 let touchMoveX = event.changedTouches[0].pageX;
 let touchMoveY = event.changedTouches[0].pageY;
 // 起始点的坐标(x0,y0)和手指离开时的坐标(x1,y1)之差
 let tmX = touchMoveX - touchDotX;
 let tmY = touchMoveY - touchDotY;
 // 两点横纵坐标差的绝对值
 let absX = Math.abs(tmX);
 let absY = Math.abs(tmY);
 //起始点的坐标(x0,y0)和手指离开时的坐标(x1,y1)之间的距离
 let delta = Math.sqrt(absX * absX + absY * absY);
 console.log('起始点和离开点距离:' + delta + 'px');
 // 如果delta超过60px(可以视情况自己微调),判定为手势触发
 if (delta >= 60) {
 // 如果 |x0-x1|>|y0-y1|,即absX>abxY,判定为左右滑动
 if (absX > absY) {
 // 如更tmX<0,即(离开点的X)-(起始点X)小于0 ,判定为左滑
 if (tmX < 0) {
  console.log("左滑=====");
  // 执行左滑动画
  this.Animation1(-500);
  // 如更tmX>0,即(离开点的X)-(起始点X)大于0 ,判定为右滑
 } else {
  console.log("右滑=====");
  // 执行右滑动画
  this.Animation1(500);
 }
 // 如果 |x0-x1|<|y0-y1|,即absX<abxY,判定为上下滑动
 } else {
 // 如更tmY<0,即(离开点的Y)-(起始点Y)小于0 ,判定为上滑
 if (tmY < 0) {
  console.log("上滑动=====");
  this.setData({
  isFront1: !this.data.isFront1
  });
  // 如更tmY>0,即(离开点的Y)-(起始点Y)大于0 ,判定为下滑
 } else {
  console.log("下滑动=====");
  this.setData({
  isFront1: !this.data.isFront1
  });
 }
 }
 } else {
 console.log("手势未触发=====");
 }
 
 // 让上一张卡片展现正面(如果之前翻转过的话)
 this.setData({
 isFront3: true,
 });
 }

为了更直观的看到手势触发的条件,我画了一张图:

怎么在微信小程序中实现一个手势滑动卡片效果

图片(二)

最后是动画的编写;

上代码:

/**
 * 卡片1:
 * 左滑动右滑动动画
 */
 Animation1: function (translateXX) {
 let animation = wx.createAnimation({
 duration: 680,
 timingFunction: "ease",
 });
 this.animation = animation;
 // 如果大于0,判定是右滑动画,否则左滑
 if (translateXX > 0) {
 this.animation.translateY(0).rotate(20).translateX(translateXX).opacity(0).step();
 } else {
 this.animation.translateY(0).rotate(-20).translateX(translateXX).opacity(0).step();
 }
 // 设置10ms,视觉欺骗,直接归位到原来位置
 this.animation.translateY(0).translateX(0).opacity(1).rotate(0).step({
 duration: 10
 });
 
 this.setData({
 animationData1: this.animation.export(),
 });
 // 动画结束后重拍三张卡片
 setTimeout(() => {
 this.setData({
 ballTop1: 220,
 ballLeft1: -302.5,
 ballWidth2: 605,
 index1: 1,
 
 ballTop2: 240,
 ballLeft2: -340,
 ballWidth3: 680,
 index2: 3,
 
 ballTop3: 230,
 ballLeft3: -320,
 ballWidth4: 640,
 index3: 2,
 })
 }, 500);
 }

关于怎么在微信小程序中实现一个手势滑动卡片效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 怎么在微信小程序中实现一个全选多选效果
  2. 怎么在微信小程序中实现一个图片翻转效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

微信小程序

上一篇:怎么在小程序中实现一个层叠卡片滑动效果

下一篇:使用vue怎么实现一个柱状进度条图像

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》