当前位置:首页 > 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 动态计算

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

<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 组件。

安装依赖:

npm install vue-marquee-text-component

使用示例:

vue实现无缝marquee

<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实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在 V…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show 通过…