vue2封装向上滚动组件

1.代码

<template>
  <div class="marquee-wrap" :style="{'height': height + 'px'}">
    <ul class="marquee-list"
        :style="animateUpStyle"
        v-on:mouseover="myMouseover"
        v-on:mouseout="myMouseout"
    >
      <li v-for="(item, index) in listData" :key="index"
          :style="{'height': height + 'px', 'line-height': height + 'px'}">
        <slot :scrollItem="item"></slot>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "TextScrolling",
  props: {
    scrollData: {
      type: Array,
    },
    height: {
      type: Number,
      default: 40
    },
    delay: {
      type: Number,
      default: 500
    }
  },
  data() {
    return {
      animateUp: false,
      listData: [],
      timer: null
    }
  },
  mounted() {
    this.timer = setInterval(this.scrollAnimate, this.delay + 1000);
  },
  computed: {
    scrollDataProp() {
      return this.scrollData;
    },
    transitionDelay() {
      return this.delay / 1000
    },
    animateUpStyle() {
      if (this.animateUp) {
        return {
          transition: `all ${this.transitionDelay}s ease-in-out`,
          transform: `translateY(-${this.height}px)`
        }
      } else {
        return {}
      }
    }
  },
  watch: {
    scrollDataProp: {
      handler(newVal) {
        this.listData = newVal
      },
      immediate: true
    }
  },
  methods: {
    scrollAnimate() {
      this.animateUp = true
      setTimeout(() => {
        this.listData.push(this.listData[0])
        this.listData.shift()
        this.animateUp = false
      }, this.delay)
    },
    myMouseover() {
      clearInterval(this.timer)
    },
    myMouseout() {
      this.timer = setInterval(this.scrollAnimate, this.delay + 1000);
    }
  },
  destroyed() {
    clearInterval(this.timer)
  }
}
</script>

<style scoped lang="less">
.marquee-wrap {
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;

  .marquee-list {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0;

    li {
      width: 100%;
      height: 100%;
      list-style: none;
    }
  }
}
</style>

2.使用

<text-scrolling :scrollData="scrollData" :height="40" :delay="1000">
        <template v-slot:default="scrollItem">
          <div class="scroll-box" style="color: red;border: 1px solid red;text-align: center">
            {{ scrollItem.scrollItem}} + 1
          </div>
        </template>
</text-scrolling>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值