微信小程序使用轮播组件swiper,添加左右滑动按钮

时间:2025-04-02 17:44:52
  • Page({
  • /**
  • * 页面的初始数据
  • */
  • data: {
  • list: [],
  • swiper: {},
  • },
  • // 上一页
  • prevSwiper() {
  • let swiper = this.data.swiper;
  • let current = swiper.current;
  • swiper.current = current > 0 ? current - 1 : this.data.list.length - 1;
  • this.setData({
  • swiper
  • })
  • },
  • // 下一页
  • 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();
  • }
  • })