微信小程序輪播圖


微信小程序輪播圖實現,比Android 輪播圖來說,顯得輕松多了。
微信小程序提供swiper組件,官網api提供的swiper滑塊視圖容器。

屬性名 類型 默認值 說明
autoplay Boolean false 是否自動切換
current Number 0 當前所在頁面的 index
interval Number 5000 自動切換時間間隔
duration Number 500 滑動動畫時長
circular Boolean false 是否采用銜接滑動
vertical Boolean false 滑動方向是否為縱向
bindchange EventHandle     current 改變時會觸發 change 事件,event.detail = {current: current, source: source}

從公共庫v1.4.0開始,change事件返回detail中包含一個source字段,表示導致變更的原因,可能值如下:

  • autoplay 自動播放導致swiper變化;

  • touch 用戶划動引起swiper變化;

  • 其他原因將用空字符串表示。

注意:其中只可放置<swiper-item/>組件,否則會導致未定義的行為。

swiper-item

僅可放置在<swiper/>組件中,寬高自動設置為100%。

index.wxss

swiper {

    height: 421.5rpx;

}

swiper-item image {

    width: 100%;

    height: 100%;

}

.swiper-container{

  position: relative;

}

.swiper-container .swiper{

  height: 300rpx;

}

.swiper-container .swiper .img{

  width: 100%;

  height: 100%;

}

 

index.js

 

Page({

  data: {

    swiperCurrent: 0,

    indicatorDots: true,

    autoplay: true,

    interval: 3000,

    duration: 800,

    circular:true,

    imgUrls: [

      'https://p3.pstatp.com/large/43700001e49d85d3ab52',

      'https://p3.pstatp.com/large/39f600038907bf3b9c96',

      'https://p3.pstatp.com/large/31fa0003ed7228adf421'

    ],

    links:[

      '../user/user',

      '../user/user',

      '../user/user'

    ]

    

  },

  //輪播圖的切換事件

  swiperChange: function (e) {

    this.setData({

      swiperCurrent: e.detail.current

    })

  },

  //點擊指示點切換

  chuangEvent: function (e) {

    this.setData({

      swiperCurrent: e.currentTarget.id

    })

  },

  //點擊圖片觸發事件

  swipclick: function (e) {

    console.log(this.data.swiperCurrent);

    wx.switchTab({

      url: this.data.links[this.data.swiperCurrent]

    })

  }

})

 

index.wxml

<view class="swiper-container">

  <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" circular="{{duration}}" current="{{swiperCurrent}}" bindchange="swiperChange" class="swiper">

    <block wx:for="{{imgUrls}}" wx:key="unique">

      <swiper-item>

        <image src="{{item}}" class="img" bindtap="swipclick" />

      </swiper-item>

    </block>

  </swiper>

</view>

 

重要一點是圖片的點擊事件。bindtap="swipclick"

swipclick: function (e) {

    console.log(this.data.swiperCurrent);

    wx.switchTab({

      url: this.data.links[this.data.swiperCurrent]

    })

  }

0?wx_fmt=jpeg

 

 

0?wx_fmt=gif


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM