微信小程式swiper實現 句子控app首頁滑動卡片

Mr、Kr發表於2021-02-09

微信小程式swiper實現 句子控app首頁滑動卡片

引言:最近看到句子控APP首頁的效果很清新,可是發現他的微信小程式端沒有實現這個功能,我看了一下難度不大,於是嘗試著去實現。

實現效果如下:

 

 

1、定義一個yiyancard自定義元件,在根目錄下新建一個components資料夾並在其內部新建一個yiyancard資料夾。

 

2、在pages資料夾下新建一個home頁面

3、在home頁面的json引入yiyancard元件,並在wxml中使用

index.json

{
  "usingComponents": {
    "s-yiyancard": "../../components/yiyancard/index"

  }
}

index.wxml

<view class="container">
  <s-yiyancard style="width:100vw"> </s-yiyancard>
</view>

4、編寫yiyancard相關程式碼

index.js

Page({
  lifetimes: {
    attached: function () {
      this.setData({
        cardCur: 0
      })
    },
    detached: function () {
      // 在元件例項被從頁面節點樹移除時執行
    },
  },
  pageLifetimes: {
    show: function () {
      this.setData({
        cardCur: 0
      })
    },
  },
  data: {

    cardCur: 0,
    swiperList: [ {id: 0,
      type: 'image',
      yiyan: '沒有無聊的人生,只有無聊的人生態度',
      form: '劉瑜',
      tag: "勵志雞湯句子",
      liuyannum: "36",
      likenum: "2k",

      iflike:"false"

    }, {
      id: 1,
      type: 'image',
      url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/1.JPG?sign=6a578b89d06a74141a01b35b26684e04&t=1612536951',
      yiyan: '沒有任何一種逃避能得到讚賞,喜歡就去追,餓了就吃飯,管他是失敗或是發胖',
      form: '',
      likenum: "17K",
      liuyannum: "126",
      iflike:"false"

    }, {
      id: 2,
      type: 'image',
      url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/2.JPG?sign=42c68e97e5fcd277e42bdc476a94cdb4&t=1612536964',
      yiyan: '萬物皆有裂痕,那是光進來的地方',
      tag: "勵志",
      likenum: "11K",
      liuyannum: "31",
      iflike:"false"

    }, {
      id: 3,
      type: 'image',
      url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/3.JPG?sign=d607ee45937f227f8ae5fba4e9f846f4&t=1612536974',
      yiyan: '不聽命於自己者,就要受命於他人',

      form: '尼采',
      likenum: "1.0K",
      liuyannum: "112",
      iflike:"false"

    }, {
      id: 4,
      type: 'image',
      url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/4.JPG?sign=4c986f27559352bb5d9b42a96851ab22&t=1612536983',
      yiyan: '我從不曾崩潰瓦解,因為我從不曾完好無缺',

      form: '安迪·沃霍爾',
      likenum: "967",
      liuyannum: "30",
      iflike:"false"

    }, {
      id: 5,
      type: 'image',
      url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/5.JPG?sign=22d43fff1c66594770c1c2b10cd8403c&t=1612537004',
      yiyan: '總有人找你這可小星球,瞭解你的溫柔和璀璨,即使旁邊的宇宙再浪漫',
      form: '',
      likenum: "19k",
      liuyannum: "1514",
      iflike:"false"

    }, {
      id: 5,
      type: 'image',
      url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/5.JPG?sign=22d43fff1c66594770c1c2b10cd8403c&t=1612537004',
      yiyan: '總有人找你這可小星球,瞭解你的溫柔和璀璨,即使旁邊的宇宙再浪漫',
      form: '',
      likenum: "11k",
      liuyannum: "1136",
      iflike:"false"

    }, {
      id: 5,
      type: 'image',
      url: 'https://7368-shuyuabn-9gke6t6k962d48ca-1304045188.tcb.qcloud.la/image/5.JPG?sign=22d43fff1c66594770c1c2b10cd8403c&t=1612537004',
      yiyan: '總有人找你這可小星球,瞭解你的溫柔和璀璨,即使旁邊的宇宙再浪漫',
      form: '',
      likenum: "12K",
      liuyannum: "306",
      iflike:"false"

    }, {
      id: 6,
      type: 'image',

      yiyan: '總有人找你這可小星球,瞭解你的溫柔和璀璨,即使旁邊的宇宙再浪漫',
      form: '',
      likenum: "11K",
      liuyannum: "391",
      iflike:"false"

    }, {
      ifend: true
    }],
  },
  onLoad() {
    this.towerSwiper('swiperList');
    // 初始化towerSwiper 傳已有的陣列名即可
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper觸控開始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper計算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper計算滾動
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },
  like: function (event) {
    const that = this
    let likeid = event.currentTarget.dataset.likeid
    var a = `swiperList[${likeid}].iflike`
    var b =`that.data.swiperList[${likeid}].iflike`
    console.log(a)
    console.log(b)

    if (that.data.swiperList[likeid].iflike=== "false") {
      that.setData({
      [a] : "true",
      })
    } else if (that.data.swiperList[likeid].iflike=== "true") {
      that.setData({
        [a]: "false",
      })
    }
    
    console.log(that.data.swiperList[likeid].iflike)
  }
})

 

index.json

{
  "component": true,
  "usingComponents": {}
}

index.wxss

<swiper class="card-swiper " duration="500" bindchange="cardSwiper">
  <swiper-item wx:for="{{swiperList}}" wx:key class="{{cardCur==index?'cur':''}}">
    <block wx:if="{{!item.ifend}}" >
      <view class="swiper-item other ">
      <image src="{{item.url}}" mode="aspectFill" class="swiper-img"></image>
      <view class="yiyan-body">{{item.yiyan}}</view>
      <view wx:if="{{item.form}}" class="yiyan-form">- {{item.form}} -</view>
      <view wx:if="{{!item.form}}" class="zhanwei"></view>
      <view class="bottom-box">
        <view class="like juzhong" bindtap="like" data-likeid="{{item.id}}">
          <block wx:if="{{item.iflike =='false'}}">
            <image class="icon-img" src="../../images/icon/xihuan.png"></image>
          </block>
          <block wx:if="{{item.iflike =='true'}}">
            <image class="icon-img" src="../../images/icon/xihuan2.png"></image>
          </block>
          <view class="num">{{item.likenum}}</view>
        </view>
        <view class="liuyan">
          <image class="icon-img2" src="../../images/icon/liuyan.png"></image>
          <view class="num">{{item.liuyannum}}</view>
        </view>
        <view class="biaoqian">
          <image class="icon-img" src="../../images/icon/biaoqian.png"></image>
        </view>
        <view class="zhuanfa">
          <image class="icon-img" src="../../images/icon/zhuanfa.png"></image>
        </view>
      </view>
    </view>
    </block>
    <block wx:if="{{item.ifend}}">
      <view class="swiper-item other juzhong">
        <view class="end-title">每日十句精選投稿</view>
        <view class="end-body">
          感謝支援,每天都有不同的收穫。如果意猶未盡,可以點選底部按鈕檢視更多推薦
        </view>
        <view class="end-bottom"> 去發現頁檢視更多推薦</view>
      </view>
    </block>
  </swiper-item>
</swiper>

 

index.css

/* ==================
         輪播
 ==================== */

.swiper-img {
    height: 35% !important;

}

.swiper-item image,
.swiper-item video {
    width: 100%;
    display: block;
    height: 100%;
    margin: 0;
    pointer-events: none;
}

.card-swiper {

    height: 85vh !important;
}

.card-swiper swiper-item {
    width: 610rpx !important;
    left: 70rpx;
    box-sizing: border-box;
    padding: 40rpx 0rpx 70rpx;
    overflow: initial;
}

.card-swiper swiper-item .swiper-item {
    width: 100%;
    display: block;
    height: 100%;
    border-radius: 10rpx;
    transform: scale(0.9);
    transition: all 0.2s ease-in 0s;
    overflow: hidden;
}

.card-swiper swiper-item.cur .swiper-item {
    transform: none;
    transition: all 0.2s ease-in 0s;
}

swiper {
    margin-top: 15rpx !important;
}

.other {
    position: relative;

    background-color: #ffffff;
    display: flex;
    /*flex佈局方法*/
    flex-direction: column;
    /*垂直佈局*/
    align-items: center;
    /*水平方向居中*/

    box-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.08);
    position: relative;

}

/* 內容 */
.yiyan-body {
    line-height: 46rpx;
    letter-spacing: 5rpx;
    margin-top: 38rpx;
    font-size: 30rpx;
    width: 90%;
    height: 35%;
    margin-left: 5%;
    color: #3e3e3e;
}

.yiyan-form {
    position: absolute;
    bottom: 55px;
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 25rpx;
    color: #999999;
    width: 50%;
    margin-top: 20%;
    margin-left: 25%;

}


/* 底部 */
.bottom-box {
    position: absolute;
    bottom: 0px;
    width: 100%;
    height: 80rpx;
    display: flex;
    flex-direction: row;
}

.juzhong {
    display: flex;
    justify-content: center;
    align-items: center;

}

.icon-img {
    width: 30rpx !important;
    height: 30rpx !important;
}

.icon-img2 {
    width: 30rpx !important;
    height: 30rpx !important;
    /* margin-top: 5rpx !important; */
}

.like {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 27%;
}

.num {
    font-size: smaller;
    margin-top: 3rpx;
    margin-left: 6rpx;
    color: #b4b4b4;
}

.liuyan {
    width: 27%;
    display: flex;
    justify-content: center;

    align-items: center;
}

.biaoqian {
    width: 23%;
    display: flex;
    justify-content: center;

    align-items: center;
}

.zhuanfa {
    width: 23%;
    display: flex;
    justify-content: center;

    align-items: center;
}
/* 最後一頁 */
.end-title{
    width: 60%;
    margin-left: 20%;
    font-size: 30rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 100rpx;
    margin-bottom: 100rpx;
}
.end-body{
    width: 80%;
    margin-left: 10%;
    font-size: 27rpx;
    color: #a7a7a7;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 40rpx;
}
.end-bottom{
    position: absolute;
    width: 70%;
    left: 15%;
    bottom: 60rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 40rpx;
    color: #7b9fcb;
    font-size: 28rpx;
}

 

編寫完以上程式碼,相關功能就實現了。

注意:為了方便以上data中的資料是直接在js中自定義好的。關於動態改變喜歡圖示詳情參見上一篇隨筆。

 

相關文章