溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在微信小程序中實現(xiàn)一個手勢滑動卡片效果

發(fā)布時間:2021-04-19 17:47:36 來源:億速云 閱讀:428 作者:Leah 欄目:web開發(fā)

怎么在微信小程序中實現(xiàn)一個手勢滑動卡片效果?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

代碼:

//設(shè)置position: absolute; left:50%;后,再 margin-left:負(一半的width);可以實現(xiàn)水平居中
//同理,設(shè)置top:50%;margin-top:負(一半height);可以實現(xiàn)垂直居中
.body-swiper {
 width: 680rpx;//rpx是為了適應(yīng)屏幕
 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;
}

接下來,是卡片手勢的實現(xiàn);

上代碼:

/**
 * 卡片1手勢
 */
 touchstart1: function (event) {
 touchDotX = event.touches[0].pageX; // 獲取觸摸時的原點
 touchDotY = event.touches[0].pageY;
 console.log("起始點的坐標X:" + touchDotX);
 console.log("起始點的坐標Y:" + touchDotY);
 },
 // 移動結(jié)束處理動畫
 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(可以視情況自己微調(diào)),判定為手勢觸發(fā)
 if (delta >= 60) {
 // 如果 |x0-x1|>|y0-y1|,即absX>abxY,判定為左右滑動
 if (absX > absY) {
 // 如更tmX<0,即(離開點的X)-(起始點X)小于0 ,判定為左滑
 if (tmX < 0) {
  console.log("左滑=====");
  // 執(zhí)行左滑動畫
  this.Animation1(-500);
  // 如更tmX>0,即(離開點的X)-(起始點X)大于0 ,判定為右滑
 } else {
  console.log("右滑=====");
  // 執(zhí)行右滑動畫
  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("手勢未觸發(fā)=====");
 }
 
 // 讓上一張卡片展現(xiàn)正面(如果之前翻轉(zhuǎn)過的話)
 this.setData({
 isFront3: true,
 });
 }

為了更直觀的看到手勢觸發(fā)的條件,我畫了一張圖:

怎么在微信小程序中實現(xiàn)一個手勢滑動卡片效果

圖片(二)

最后是動畫的編寫;

上代碼:

/**
 * 卡片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();
 }
 // 設(shè)置10ms,視覺欺騙,直接歸位到原來位置
 this.animation.translateY(0).translateX(0).opacity(1).rotate(0).step({
 duration: 10
 });
 
 this.setData({
 animationData1: this.animation.export(),
 });
 // 動畫結(jié)束后重拍三張卡片
 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);
 }

關(guān)于怎么在微信小程序中實現(xiàn)一個手勢滑動卡片效果問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI