web123456

WeChat applet uses the carousel component swiper, add left and right sliding buttons

  • Page({
  • /**
  • * Initial data of the page
  • */
  • data: {
  • list: [],
  • swiper: {},
  • },
  • // Previous page
  • prevSwiper() {
  • let swiper = this.data.swiper;
  • let current = swiper.current;
  • swiper.current = current > 0 ? current - 1 : this.data.list.length - 1;
  • this.setData({
  • swiper
  • })
  • },
  • // Next page
  • nextSwiper() {
  • var swiper = this.data.swiper;
  • var current = swiper.current;
  • swiper.current = current < ( this.data.list.length - 1) ? current + 1 : 0;
  • this.setData({
  • swiper
  • })
  • },
  • onLoad(){
  • const that = this;
  • let query = wx.createSelectorQuery();
  • var swiper = that.data.swiper;
  • query.select('#swiper').boundingClientRect(function(rect) {
  • swiper.current = 0
  • that.setData({
  • swiper
  • })
  • }).exec();
  • }
  • })