vue2实现列表数据字幕式循环滚动

一、案例效果:

二、功能点:

        实现列表“字幕式”滚动效果;

        字幕循环滚动,滚完数据之后会继续从头开始滚动

        实现鼠标移入,字幕停止滚动;鼠标移出,字幕继续滚动;

        实现滚轮在指定区域内滚动,能够控制控制字幕上下滚动

三、代码:

<template>
  <div class="subtitle-container"
       ref="container"
       @mouseenter="stopScrolling"
       @mouseleave="startScrollingOnLeave"
       @wheel="handleScroll">
    <ul class="subtitle-list"
        :style="{ transform: `translateY(-${currentScrollTop}px)` }">
      <li v-for="(subtitle, index) in subtitles"
          :key="index"
          class="subtitle">{{ subtitle }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      subtitles: ['在岁月的流逝中,花开花落,却留下了永恒的思念。', '晨曦之光,穿透黑夜的深邃,唤醒沉睡的希望。', '微风轻拂,草木低语,岁月静好,心安如初。', '星空如梦,岁月如歌,心中的孤舟在悠远的彼岸徘徊。', '晨曦之光,穿透黑夜的深邃,唤醒沉睡的希望。', '微风轻拂,草木低语,岁月静好,心安如初。', '情深意浓,宛如一汪清泉,润泽心灵的枯渴。', '山水之间,一念之差,心的旅程,或是波澜壮阔,或是静谧悠长。', '浮生若梦,红尘漫漫,唯有心中的那一抹云彩,永不散去。', '星光璀璨,照亮前行的路途,让希望之花绽放在黑暗中。', '海浪拍岸,千年的沧桑,铭刻着岁月的印记,留下无尽的思绪。', '暮色渐浓,烛光摇曳,思念的火焰在心中燃烧,驱散寂寞的阴霾。'], // 假设这里是你的字幕数据
      scrollSpeed: 50, // 滚动速度,单位为毫秒
      scrollInterval: null, // 滚动间隔的引用
      containerHeight: 0, // 容器高度
      subtitleHeight: 0, // 单个字幕的高度
      currentScrollTop: 0, // 当前滚动的位置
      isHovering: false // 标记鼠标是否悬停在容器上
    };
  },
  mounted () {
    this.containerHeight = this.$refs.container.offsetHeight;
    this.subtitleHeight = this.$refs.container.querySelector('.subtitle').offsetHeight;
    this.startScrolling();
  },
  methods: {
    startScrolling () {
      if (!this.isHovering) {
        this.scrollInterval = setInterval(() => {
          this.currentScrollTop += 1;
          if (this.currentScrollTop >= this.subtitleHeight) {
            this.currentScrollTop = 0;
            this.subtitles.push(this.subtitles.shift());
          }
        }, this.scrollSpeed);
      }
    },
    stopScrolling () {
      clearInterval(this.scrollInterval);
      this.isHovering = true;
    },
    startScrollingOnLeave () {
      if (this.isHovering) {
        this.isHovering = false;
        this.startScrolling();
      }
    },
    handleScroll (event) {
      this.currentScrollTop += event.deltaY;
      if (this.currentScrollTop < 0) {
        this.currentScrollTop = 0;
      } else if (this.currentScrollTop > (this.subtitles.length * this.subtitleHeight - this.containerHeight)) {
        this.currentScrollTop = this.subtitles.length * this.subtitleHeight - this.containerHeight;
      }
    }
  },
  beforeDestroy () {
    clearInterval(this.scrollInterval); // 在组件销毁前清除滚动间隔
  }
};
</script>

<style>
.subtitle-container {
  text-align: center;
  margin: 0 auto;
  margin-top: 30px;
  overflow: hidden;
}

.subtitle-list {
  padding: 0;
  margin: 0;
  list-style-type: none;
}

.subtitle {
  line-height: 30px; /* 调整为适当的行高 */
}
</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue实现无限循环滚动列表,可以考虑使用两个相同的列表,一个列表用于渲染当前可见区域的数据,另一个列表用于渲染边界处的数据。当滚动到边界处时,将边界列表数据复制到另一个列表中,实现无限循环滚动的效果。 下面是一个简单的实现示例: ```html <template> <div class="list-container" ref="listContainer"> <div class="list" ref="list"> <div v-for="(item, index) in visibleData" :key="index"> {{ item }} </div> </div> <div class="list-clone" ref="listClone"> <div v-for="(item, index) in cloneData" :key="index"> {{ item }} </div> </div> </div> </template> <script> export default { data() { return { data: ["item1", "item2", "item3", "item4", "item5"], visibleData: [], cloneData: [] }; }, mounted() { // 初始化列表 this.updateList(); // 监听滚动事件 this.$refs.listContainer.addEventListener("scroll", this.handleScroll); }, beforeDestroy() { // 移除滚动事件监听器 this.$refs.listContainer.removeEventListener( "scroll", this.handleScroll ); }, methods: { // 更新列表数据 updateList() { const { data } = this; const visibleData = data.slice(); const cloneData = data.slice(0, 2); this.visibleData = visibleData; this.cloneData = cloneData; }, // 处理滚动事件 handleScroll() { const { scrollTop, scrollHeight, clientHeight } = this.$refs.listContainer; // 判断是否到达边界 if (scrollTop === 0) { // 滚动到顶部,将边界数据插入到列表底部 this.visibleData = this.cloneData.concat(this.data); this.$refs.list.scrollTop = this.$refs.list.scrollHeight / 3; } else if (scrollTop + clientHeight === scrollHeight) { // 滚动到底部,将边界数据插入到列表顶部 this.visibleData = this.data.concat(this.cloneData); this.$refs.list.scrollTop = this.$refs.list.scrollHeight / 3; } } } }; </script> ``` 这个示例中,我们使用了两个 `div` 元素,一个用于渲染当前可见区域的数据,另一个用于渲染边界处的数据。在 `mounted` 钩子函数中,我们初始化了列表数据,并添加了滚动事件的监听器。在 `beforeDestroy` 钩子函数中,我们移除了滚动事件的监听器。 `updateList` 方法用于更新列表数据,将数据分为可见区域数据和边界数据,并将它们分别渲染到两个 `div` 元素中。 `handleScroll` 方法用于处理滚动事件。当滚动到顶部时,我们将边界数据插入到列表底部,并将滚动位置设置为列表高度的三分之一;当滚动到底部时,我们将边界数据插入到列表顶部,并将滚动位置设置为列表高度的三分之一。 这样,就可以实现一个简单的无限循环滚动列表了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值