uniapp 自定义轮播图

一、 这里是做成一个子组件的方式,可以通过左右滑动切换轮播图,也可以自动轮播,复制代码就可以使用了

<template>
  <view class="swiperPanel" ref="swiperPanel" @touchstart="startMove" @touchend="endMove">
    <view class="swiperItem" v-for="(item, index) in imgList" :key="index" :style="itemStyle[index]">
      <img class="pic" :src="item">
    </view>
  </view>
</template>

<script>
  var timer
	export default {
		props: {
			imgList:{
        type: Array,
				default: []
      }
		},
		data() {
			return {
        itemStyle:[],
        slideNote: {
          x: 0,
          y: 0
        },
			};
		},
    computed:{
    },
		created() {
      this.imgList.forEach((item, index) => {
        this.itemStyle.push(this.getStyle(index))
      })
			timer = setInterval(() => {
				this.rightSlider()
			}, 15000)
		},
		methods: {
			// 动态轮播 把最后一个放到第一个
			rightSlider () {
				var newList = JSON.parse(JSON.stringify(this.itemStyle))
				var last = [newList.pop()]
				newList = last.concat(newList)
				this.itemStyle = newList
			},
			// 设置图片的样式
      getStyle (e) {
        if (e > this.imgList.length / 2) {
          var right = this.imgList.length - e
          return {
            transform: 'scale('+(1-right/10)+') translate(-'+right*13+'%,0px)',
            zIndex: 87 - right,
            opacity: 0.5 / right
          }
        } else {
          return {
            transform:'scale(' + (1 - e / 10) + ') translate(' + e * 13 + '%,0px)',
            zIndex: 87 - e,
            opacity: 0.5 / e
          }
        }
      },
      startMove (e) {
        clearInterval(timer)
        this.slideNote.x = e.changedTouches[0] ? e.changedTouches[0].pageX : 0
        this.slideNote.y = e.changedTouches[0] ? e.changedTouches[0].pageY : 0
      },
      endMove (e) {
        timer = setInterval(() => {
          this.rightSlider()
        }, 15000)
        var newList = JSON.parse(JSON.stringify(this.itemStyle))
        if (e.changedTouches[0].pageX - this.slideNote.x < 0) {
          // 向左滑动
          var last = [newList.pop()]
          newList = last.concat(newList)
        } else {
          // 向右滑动
          newList.push(newList[0])
          newList.splice(0, 1)
        }
        this.itemStyle = newList
      },
      closeShow(){
        this.$refs.popup.close()
      },
      openShow(){
				this.$refs.popup.open('conten')
			},
		}
	}
</script>

<style lang="scss" scoped>
  .swiperPanel {
    height: 520rpx;
    width: 550rpx;
    // overflow: hidden;
    position: relative;
    .swiperItem {
      height: 100%;
      width: 100%;
      position: absolute;
      top: 0;
      left: 0upx;
      transition: all 0.5s;
      .right {
        position: absolute;
        right: 0;
        top: 0;
        width: 50%;
        height: 100%;
        background: #c29462;
        color: #fff;
        font-size: 32upx;
        padding: 10upx;
        box-sizing: border-box;
        border-radius: 0 10upx 10upx 0;
      }
      .pic {
        height: 100%;
        width: 100%;
        border-radius: 10upx;
      }
    }
  }
</style>

 二、引入组件传入图片数组

<swipe :imgList="headImgList"></swipe>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值