当前位置:首页 > VUE

vue实现无缝marquee

2026-02-18 07:19:55VUE

Vue 实现无缝 Marquee 效果

在 Vue 中实现无缝 Marquee(跑马灯)效果可以通过多种方式完成,以下是几种常见的方法:

使用 CSS 动画实现

通过 CSS 的 @keyframesanimation 属性可以实现简单的无缝滚动效果。这种方法性能较好,适合纯文本或简单内容的滚动。

<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>

使用 JavaScript 动态更新内容

对于更复杂的内容或需要动态更新的情况,可以使用 JavaScript 动态计算和更新内容位置。

vue实现无缝marquee

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

<script>
export default {
  data() {
    return {
      text: '这是一段需要无缝滚动的文本内容。',
      position: 0,
      animationId: null
    }
  },
  mounted() {
    this.startAnimation()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  },
  methods: {
    startAnimation() {
      const containerWidth = this.$refs.container.offsetWidth
      const contentWidth = this.$refs.content.offsetWidth

      const animate = () => {
        this.position -= 1
        if (this.position <= -contentWidth) {
          this.position = containerWidth
        }
        this.$refs.content.style.transform = `translateX(${this.position}px)`
        this.animationId = requestAnimationFrame(animate)
      }
      animate()
    }
  }
}
</script>

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

.marquee-content {
  display: inline-block;
}
</style>

使用第三方库

如果需要更复杂的功能或更好的性能,可以考虑使用第三方库如 vue-marquee-text-component

安装:

vue实现无缝marquee

npm install vue-marquee-text-component

使用:

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

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

export default {
  components: {
    MarqueeText
  },
  data() {
    return {
      text: '这是一段需要无缝滚动的文本内容。'
    }
  }
}
</script>

实现双向无缝滚动

如果需要内容在容器内双向无缝滚动,可以通过复制内容并交替显示实现。

<template>
  <div class="marquee-container">
    <div class="marquee-content" :style="{ transform: `translateX(${position}px)` }">
      {{ text }} {{ text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '这是一段需要无缝滚动的文本内容。',
      position: 0,
      animationId: null
    }
  },
  mounted() {
    this.startAnimation()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  },
  methods: {
    startAnimation() {
      const animate = () => {
        this.position -= 1
        if (this.position <= -this.$el.offsetWidth) {
          this.position = 0
        }
        this.animationId = requestAnimationFrame(animate)
      }
      animate()
    }
  }
}
</script>

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

.marquee-content {
  display: inline-block;
}
</style>

以上方法可以根据具体需求选择适合的实现方式。CSS 动画适合简单场景,JavaScript 实现更灵活,第三方库则提供更多功能。

标签: vuemarquee
分享给朋友:

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现…

vue 实现tabs

vue 实现tabs

Vue 实现 Tabs 组件的方法 使用动态组件和 v-for 在 Vue 中实现 Tabs 可以通过动态组件和 v-for 指令结合完成。定义一个 tabs 数组,包含每个标签的标题和对应的组件。…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense…