温馨提示×

温馨提示×

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

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

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

发布时间:2021-04-19 17:47:36 阅读:448 作者:Leah 栏目:web开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

代码:

//设置position: absolute; left50%;后,再 margin-left:负(一半的width);可以实现水平居中
//同理,设置top:50%;margin-top:负(一半height);可以实现垂直居中
.body-swiper {
 width680rpx;//rpx是为了适应屏幕
 height900rpx;
 left50%;
 position: absolute;
 margin-left: -340rpx;
 z-index3;
 box-sizing: border-box;
 -webkit-box-shadow0 1px 4px rgba(0000.62), 0 0 60px rgba(0000.06) inset;
 -moz-box-shadow0 1px 4px rgba(0000.62), 0 0 40px rgba(0000.06) inset;
 box-shadow0 1px 4px rgba(0000.62), 0 0 40px rgba(0000.06) inset;
 border-radius12px;
}
 
.body-swiper2 {
 width640rpx;
 height900rpx;
 left50%;
 position: absolute;
 margin-left: -320rpx;
 z-index2;
 box-sizing: border-box;
 -webkit-box-shadow0 1px 4px rgba(0000.62), 0 0 60px rgba(0000.06) inset;
 -moz-box-shadow0 1px 4px rgba(0000.62), 0 0 40px rgba(0000.06) inset;
 box-shadow0 1px 4px rgba(0000.62), 0 0 40px rgba(0000.06) inset;
 border-radius12px;
}
 
.body-swiper3 {
 width605rpx;
 height900rpx;
 left50%;
 position: absolute;
 margin-left: -302.5rpx;
 z-index1;
 box-sizing: border-box;
 -webkit-box-shadow0 1px 4px rgba(0000.62), 0 0 60px rgba(0000.06) inset;
 -moz-box-shadow0 1px 4px rgba(0000.62), 0 0 40px rgba(0000.06) inset;
 box-shadow0 1px 4px rgba(0000.62), 0 0 40px rgba(0000.06) inset;
 border-radius12px;
}

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

上代码:

/**
 * 卡片1手势
 */
 touchstart1function (event) {
 touchDotX = event.touches[0].pageX// 获取触摸时的原点
 touchDotY = event.touches[0].pageY;
 console.log("起始点的坐标X:" + touchDotX);
 console.log("起始点的坐标Y:" + touchDotY);
 },
 // 移动结束处理动画
 touchend1function (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({
 isFront3true,
 });
 }

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

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

图片(二)

最后是动画的编写;

上代码:

/**
 * 卡片1:
 * 左滑动右滑动动画
 */
 Animation1: function (translateXX{
 let animation = wx.createAnimation({
 duration680,
 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({
 duration10
 });
 
 this.setData({
 animationData1: this.animation.export(),
 });
 // 动画结束后重拍三张卡片
 setTimeout(() => {
 this.setData({
 ballTop1220,
 ballLeft1: -302.5,
 ballWidth2605,
 index11,
 
 ballTop2240,
 ballLeft2: -340,
 ballWidth3680,
 index23,
 
 ballTop3230,
 ballLeft3: -320,
 ballWidth4640,
 index32,
 })
 }, 500);
 }

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

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

AI

开发者交流群×