VUE上下轮播实现

 

<template>
<div>
  <div
    :style="{ height: height * lineNum + 'px' }"
    class="rollScreen_container"
    id="rollScreen_container"
  >
    <ul
      class="rollScreen_list"
      :style="{ transform: transform }"
      :class="{ rollScreen_list_unanim: num === 0 }"
    >
      <li class="rollScreen_once" v-for="(item,index) in contentArr" :key='index' :style="{height:height+'px',lineHeight:height+'px'}">
        <img :src="item" class="img"></img>
      </li>
      <slot name="slide"></slot>
      <!-- <li
        class="rollScreen_once"
        v-for="(item, index) in contentArr"
        :key="index + contentArr.length"
        :style="{ height: height + 'px' }"
      >
        <span>{{ item }}</span>
      </li> -->
    </ul>
  </div>
      <p @click="clickBtn">点击关闭轮播</p>

  </div>
</template>
 
<script>
export default {
  props: {
    // height: {
    //   default: 40,
    //   type: Number,
    //   required: true
    // }, // 每行元素的高度
    // lineNum: {
    //   default: 5,
    //   type: Number
    // }, // 显示行数
    // contentArr: {
    //   default: [
    //     'https://img01.yzcdn.cn/vant/apple-1.jpg',
    //     'https://img01.yzcdn.cn/vant/apple-2.jpg',
    //     'https://img01.yzcdn.cn/vant/apple-1.jpg',
    //     'https://img01.yzcdn.cn/vant/apple-2.jpg',
    //     'https://img01.yzcdn.cn/vant/apple-1.jpg',
    //   ],
    //   type: Array
    // }, // 简单文本轮播
    // dLength: {
    //   default: null,
    //   type: Number
    // }, // 自定义插槽内容的时候必须传自定义内容的长度
    // time: {
    //   default: 3000,
    //   type: Number // 定义轮播切换速度
    // }
  },
  data: function () {
    return {
      time: 1000,
      height: 100,
      lineNum: 3,
      contentArr: [
        'https://img01.yzcdn.cn/vant/apple-1.jpg',
        'https://img01.yzcdn.cn/vant/apple-2.jpg',
        'https://img01.yzcdn.cn/vant/apple-3.jpg',
        'https://img01.yzcdn.cn/vant/apple-4.jpg',
        'https://img01.yzcdn.cn/vant/apple-5.jpg',
        'https://img01.yzcdn.cn/vant/apple-6.jpg',

      ],
      num: 0,
      loopTime: 1000,
      interval: null
    }
  },
  computed: {
    transform: function () {
      console.log(this.num)
      return 'translateY(-' + this.num * this.height + 'px)'
    },
  },
  watch: {

  },
  methods: {
    clickBtn () {
      console.log('点击关闭轮播')
      this.interval && clearInterval(this.interval);//停止
    }
  },
  mounted: function () {

    let _this = this
    if (this.contentArr != null && this.contentArr.length < this.lineNum) {
      console.error('轮播显示行数不能超过数据总行数')
    } else {
      // 先判断轮播切换速度,如果小于动画播放时间则提示切换速度过快
      // if (_this.loopTime <= 1000) {
      //   console.warn('轮播切换速度过快,至少大于1s')
      //   _this.loopTime = 1000
      // }
      if (_this.contentArr !== null) {
        console.log('hhh ')
        var arr2 = _this.contentArr.shift()
        var b = [..._this.contentArr]
        _this.contentArr = b.concat(arr2)
        console.log(_this.contentArr, '_this.contentArr')
        this.interval = setInterval(function () {
          _this.num++
        }, _this.loopTime)

      }
    }
  },
  beforeDestroy () {
    this.interval && clearInterval(this.interval);//停止
  },
}
</script>
 
<style>
.rollScreen_once {
  /* margin-top: 20px; */
  /* background: red; */
}
.rollScreen_container {
  display: inline-block;
  position: relative;
  overflow: hidden;
}
.rollScreen_list {
  transition: 1s linear;
}
.rollScreen_list_unanim {
  transition: none;
}
.img {
  width: 100px;
  height: 100px;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值