vue文字间歇无缝向上滚动

公司的管理系统中有“文字间歇无缝向上滚动”的需求,下面是原生的js无需安装插件直接上代码

<div class="scroll-up" ref="scroll">
      <ul>
        <li v-for="item in scrollData" :key="item.id"><router-link to="">{{item.title}}</router-link></li>
      </ul>
    </div>
export default {
  data() {
    return {
      scrollData: [
        { id: 1, title: '架不住公司高层喜欢这种玩意儿' },
        { id: 2, title: '用在vue项目中的需求跟原生js的实现方法' },
        { id: 3, title: '文字间歇无缝向上滚动' },
        { id: 4, title: '即使你有一百个不乐意,谁让是人家给你发工资呢' },
        { id: 5, title: '今天就单拎出来一种实现方法' },
      ],
      scrollArea: '',
      speed: 20,
      timer: null,
      delay: 3000,
      liHeight: '',
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.scrollArea = this.$refs.scroll;
      let li = this.scrollArea.getElementsByTagName("li");
      this.liHeight = li[0].offsetHeight;
      this.scrollArea.scrollTop = 0;
      this.scrollArea.innerHTML += this.scrollArea.innerHTML;

      this. scrollData.length > 1 && setTimeout(this.startScroll, this.delay);
    })
  },
  methods: {
    startScroll(){
      this.timer = setInterval(this.scrollUp, this.speed);
      this.scrollArea.scrollTop++;
    },
    scrollUp(){
      if(this.scrollArea.scrollTop % this.liHeight == 0){
        clearInterval(this.timer);
        setTimeout(this.startScroll, this.delay);
      }else{
        this.scrollArea.scrollTop++;
        if(this.scrollArea.scrollTop >= this.scrollArea.scrollHeight / 2){
          this.scrollArea.scrollTop = 0;
        }
      }
    },
  }
}
<style scoped>
.scroll-up{height:50px;line-height:50px;overflow:hidden;}
</style>

转载自:https://www.cnblogs.com/tnnyang/p/11613654.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值