当前位置:首页 > VUE

vue跑马灯实现

2026-01-20 07:40:44VUE

使用 CSS 动画实现跑马灯

通过 CSS 的 @keyframestransform 属性实现水平滚动的跑马灯效果。定义一个动画,让内容从右向左移动。

<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 计算内容宽度并动态设置动画时长。

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

<script>
export default {
  data() {
    return {
      text: '动态宽度内容,动画时长会根据内容长度自动调整。'
    }
  },
  mounted() {
    this.adjustAnimationDuration();
  },
  methods: {
    adjustAnimationDuration() {
      const containerWidth = this.$refs.container.offsetWidth;
      const contentWidth = this.$refs.content.offsetWidth;
      const duration = (contentWidth / containerWidth) * 5; // 基础时长比例
      this.$refs.content.style.animationDuration = `${duration}s`;
    }
  }
}
</script>

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

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

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

无缝循环跑马灯

通过复制一份内容并交替动画,实现无缝循环效果。

<template>
  <div class="marquee-container">
    <div class="marquee-wrapper">
      <div class="marquee-content" v-for="(item, index) in 2" :key="index">
        {{ text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '无缝循环跑马灯效果,内容会连续滚动不中断。'
    }
  }
}
</script>

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

.marquee-wrapper {
  display: flex;
  width: 200%; /* 双倍宽度容纳两份内容 */
}

.marquee-content {
  flex: 1;
  white-space: nowrap;
  animation: marquee 10s linear infinite;
}

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

垂直滚动跑马灯

修改动画方向实现垂直滚动效果。

<template>
  <div class="marquee-vertical">
    <div class="marquee-content">
      <div v-for="(item, index) in items" :key="index">{{ item }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        '第一条消息',
        '第二条消息',
        '第三条消息',
        '第四条消息'
      ]
    }
  }
}
</script>

<style>
.marquee-vertical {
  height: 200px;
  overflow: hidden;
}

.marquee-content {
  animation: marquee-vertical 10s linear infinite;
}

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

使用第三方库 vue-marquee

对于更复杂的需求,可以使用专门的 Vue 跑马灯组件库。

安装依赖:

npm install vue-marquee-text-component

示例代码:

<template>
  <marquee-text :duration="20" :repeat="3">
    使用第三方库实现的跑马灯效果,支持更多配置选项。
  </marquee-text>
</template>

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

export default {
  components: {
    MarqueeText
  }
}
</script>

以上方法涵盖了从简单到复杂的多种跑马灯实现方式,可根据项目需求选择合适方案。CSS 动画方案性能较好,适合简单需求;JavaScript 方案更灵活;第三方库则提供开箱即用的丰富功能。

vue跑马灯实现

标签: 跑马灯vue
分享给朋友:

相关文章

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <…