当前位置:首页 > VUE

vue实现无缝marquee

2026-03-09 20:19:53VUE

Vue 实现无缝 Marquee 效果

使用 CSS3 Animation

通过 CSS3 的 animation@keyframes 实现无缝滚动效果,适用于简单的文字或内容滚动。

<template>
  <div class="marquee-container">
    <div class="marquee-content">{{ text }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '这是一段需要无缝滚动的文本内容,可以根据需求调整长度。'
    }
  }
}
</script>

<style>
.marquee-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.marquee-content {
  display: inline-block;
  animation: marquee 10s linear infinite;
}

@keyframes marquee {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}
</style>

使用 Vue 动态计算

动态计算内容宽度,实现更精确的无缝滚动,适用于内容长度不固定的情况。

vue实现无缝marquee

<template>
  <div class="marquee-wrapper" ref="wrapper">
    <div class="marquee-content" ref="content" :style="contentStyle">
      {{ text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '动态计算宽度的无缝滚动内容',
      contentWidth: 0,
      wrapperWidth: 0,
      duration: 10,
      contentStyle: {}
    }
  },
  mounted() {
    this.calculateWidth()
    this.startAnimation()
    window.addEventListener('resize', this.calculateWidth)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calculateWidth)
  },
  methods: {
    calculateWidth() {
      this.wrapperWidth = this.$refs.wrapper.offsetWidth
      this.contentWidth = this.$refs.content.offsetWidth
      this.contentStyle = {
        animationDuration: `${this.duration}s`,
        transform: `translateX(${this.wrapperWidth}px)`
      }
    },
    startAnimation() {
      this.contentStyle = {
        ...this.contentStyle,
        animation: `marquee ${this.duration}s linear infinite`
      }
    }
  }
}
</script>

<style>
.marquee-wrapper {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.marquee-content {
  display: inline-block;
}

@keyframes marquee {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}
</style>

使用第三方库 vue-marquee-text-component

对于更复杂的需求,可以使用专门的无缝滚动 Vue 组件。

vue实现无缝marquee

安装依赖:

npm install vue-marquee-text-component

使用示例:

<template>
  <marquee-text :duration="duration" :repeat="repeat">
    {{ text }}
  </marquee-text>
</template>

<script>
import MarqueeText from 'vue-marquee-text-component'

export default {
  components: {
    MarqueeText
  },
  data() {
    return {
      text: '使用第三方库实现的无缝滚动效果',
      duration: 15,
      repeat: 4
    }
  }
}
</script>

注意事项

  • 确保容器有明确的宽度限制,否则滚动效果可能不正常
  • 对于动态内容,需要在内容更新后重新计算宽度
  • 移动端设备可能需要调整动画持续时间以获得更好的用户体验
  • 考虑性能影响,特别是对于大量内容的滚动场景

以上方法可以根据具体需求选择使用,CSS3 Animation 适合简单场景,动态计算适合精确控制,第三方库则提供了更多配置选项和功能。

标签: vuemarquee
分享给朋友:

相关文章

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue 实现音乐

vue 实现音乐

Vue 实现音乐播放功能 在 Vue 中实现音乐播放功能,可以通过 HTML5 的 <audio> 元素或第三方库如 howler.js 来实现。以下是两种方法的详细步骤: 使用 HTM…

vue实现书库

vue实现书库

Vue 实现书库功能 使用 Vue 构建书库功能需要结合前端框架和后端数据管理。以下是实现步骤和关键点: 项目初始化 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 和状…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…