小程序中实现轮播图左向堆叠

1、效果图:

轮播图左向堆叠

2、封装的组件:

my-swiper.wxml

html 复制代码
<view>
  <view class="tower-swiper" bindtouchend="TowerEnd">
    <view class="tower-item" wx:for="{{swiperList}}" wx:key="index" style="transform: scale(calc(0.4 + {{item.zIndex}} / 5));margin-left:calc({{item.mLeft}} * 22rpx); z-index: {{item.zIndex}};">
      <view class="swiper-item">
        <image src="{{item.url}}" mode="aspectFill" wx:if="{{item.type=='image'}}"></image>
      </view>
    </view>
  </view>
</view>

my-swiper.json

html 复制代码
{
  "component": true,
  "usingComponents": {}
}

my-swiper.wxss

css 复制代码
.tower-swiper {
  width: 50rpx;
  position: relative;
}

.tower-swiper .tower-item {
  position: absolute;
  width: 50rpx;
  height: 50rpx;
  top: 0;
  bottom: 0;
  left: 50%;
  margin: auto;
  transition: all 0.2s ease-in 0s;
  opacity: 1;
}

.tower-swiper .tower-item .none {
  opacity: 0;
}
.swiper-item image {
  width: 50rpx;
  height: 50rpx;
  border-radius: 50%;
}

my-swiper.js

javascript 复制代码
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    swiperList: {
      type: Array,
      value: [
        {
          id: 0,
          type: 'image',
          url: 'https://s3.cn-northwest-1.amazonaws.com.cn/amemori-s3-cn-northwest-1/ImagesFolder/6dcecb85a997478d8aa27045195633c0.png'
        },
        {
          id: 1,
          type: 'image',
          url: 'https://s3.cn-northwest-1.amazonaws.com.cn/amemori-s3-cn-northwest-1/ImagesFolder/5fc17d5232a84bdc9a43f72300a15ec1.png',
        }, {
          id: 2,
          type: 'image',
          url: 'https://s3.cn-northwest-1.amazonaws.com.cn/amemori-s3-cn-northwest-1/ImagesFolder/c236b580936a4af1a16d6e29ed8d2e1d.png'
        },
        // {
        //   id: 3,
        //   type: 'image',
        //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg'
        // },
        // {
        //   id: 4,
        //   type: 'image',
        //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
        // }, 
        // {
        //   id: 5,
        //   type: 'image',
        //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big21016.jpg'
        // }, 
        // {
        //   id: 6,
        //   type: 'image',
        //   url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg'
        // }
      ]
    },
    towerStart: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    timer: null,
    towerStart: 0,
    direction: 'right'
  },
  lifetimes: {
    attached() {
      this.TowerSwiper()
      this.swiperOn()
    },
    detached() {
      clearInterval(this.data.timer); // 清除定时器
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    swiperOn() {
      const _this = this
      let timer = this.data.timer
      if (!timer) {
        timer = setInterval(() => {
          _this.TowerEnd()
        }, 2000)
        this.setData({
          timer
        })
      }
    },
    // 初始化towerSwiper
    TowerSwiper() {
      let list = this.data.swiperList;
      for (let i = 0; i < list.length; i++) {
       // 如果是list.length/2 :当前项的zIndex 的计算方式是:轮播图总数的一半加一,再减去当前项到中间项的距离(即绝对值的差) 这样,中间项的 zIndex 最大,其他项的 zIndex 随着距离中间项的远近逐渐减小。
        //在这里我需要逐次向左变大,而不是中间大两遍小,所以我改成了list.length / 1
        list[i].zIndex = parseInt(list.length / 1) + 1 - Math.abs(i - parseInt(list.length / 1))
        list[i].mLeft = i - parseInt(list.length / 1)
      }
      this.setData({
        swiperList: list
      })
    },
    // towerSwiper计算滚动
    TowerEnd() {
      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 < this.data.swiperList.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
      } 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({
        direction,
        swiperList: list
      })
    },
  }
})

在父组件中使用:

javascript 复制代码
{
  "usingComponents": {
    "mySwiper":"../component/my-swiper/my-swiper"
  }
}
html 复制代码
<mySwiper swiperList="{{activeAvatars}}"></mySwiper>

结束!!!

参考文章:https://mstzf.cn/posts/mp-tower-swiper/index.html

相关推荐
Y_3_74 小时前
微信小程序动态二维码外部实时展示系统
微信小程序·小程序·notepad++
weixin_1772972206919 小时前
旧物二手回收小程序:引领绿色消费,开启时尚生活新方式
小程序·盲盒
2501_9160074720 小时前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
韩立学长20 小时前
【开题答辩实录分享】以《植物病虫害在线答疑小程序的设计与实现》为例进行答辩实录分享
spring boot·小程序·vue
好想早点睡.21 小时前
vue2+UniApp微信小程序集成高德地图
微信小程序·小程序·uni-app
2501_915918411 天前
iOS 上架应用市场全流程指南,App Store 审核机制、证书管理与跨平台免 Mac 上传发布方案(含开心上架实战)
android·macos·ios·小程序·uni-app·cocoa·iphone
Mr.Aholic1 天前
分享几个开源的系统,包括小程序、商城系统、二手交易等常见的系统、很容易进行二次开发 【可以参考学习】
微信小程序·小程序·毕业设计·课程设计
2501_915909061 天前
HTTPS 错误排查实战,从握手到应用层的工程化流程
网络协议·http·ios·小程序·https·uni-app·iphone
JIngJaneIL1 天前
口腔健康系统|口腔医疗|基于java和小程序的口腔健康系统小程序设计与实现(源码+数据库+文档)
java·数据库·spring boot·小程序·论文·毕设·口腔医疗小程序
小光学长2 天前
基于微信小程序的背单词系统x1o5sz72(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·微信小程序·小程序