当前位置:首页 > VUE

vue实现文字滚动停顿

2026-02-22 01:13:46VUE

Vue实现文字滚动停顿效果

在Vue中实现文字滚动并带有停顿效果,可以通过CSS动画或JavaScript定时器实现。以下是两种常用方法:

方法一:使用CSS动画

通过CSS的@keyframesanimation属性实现滚动效果,结合animation-delay实现停顿。

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

<script>
export default {
  data() {
    return {
      text: "这是一段需要滚动显示的文字内容..."
    }
  }
}
</script>

<style>
.scroll-container {
  width: 300px;
  height: 40px;
  overflow: hidden;
  position: relative;
}

.scroll-text {
  position: absolute;
  white-space: nowrap;
  animation: scroll 10s linear infinite;
}

@keyframes scroll {
  0%, 10% {
    transform: translateX(100%);
  }
  90%, 100% {
    transform: translateX(-100%);
  }
}
</style>

方法二:使用JavaScript定时器

通过Vue的setIntervalclearInterval控制滚动和停顿时间。

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

<script>
export default {
  data() {
    return {
      text: "这是一段需要滚动显示的文字内容...",
      position: 0,
      direction: -1,
      timer: null,
      containerWidth: 0,
      textWidth: 0
    }
  },
  mounted() {
    this.initScroll();
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    initScroll() {
      this.containerWidth = this.$refs.container.offsetWidth;
      const textElement = this.$refs.container.querySelector('.scroll-text');
      this.textWidth = textElement.scrollWidth;

      this.timer = setInterval(() => {
        if (this.position <= -this.textWidth) {
          this.position = this.containerWidth;
          setTimeout(() => {
            this.startScrolling();
          }, 2000); // 停顿2秒
        } else {
          this.position += this.direction * 2;
        }
      }, 50);
    },
    startScrolling() {
      // 重新开始滚动
    }
  }
}
</script>

<style>
.scroll-container {
  width: 300px;
  height: 40px;
  overflow: hidden;
  position: relative;
}

.scroll-text {
  position: absolute;
  white-space: nowrap;
}
</style>

方法三:使用第三方库

可以借助vue-marquee等现成组件快速实现:

  1. 安装vue-marquee

    npm install vue-marquee-text-component
  2. 使用组件

    
    <template>
    <vue-marquee 
     :duration="10" 
     :repeat="4" 
     :paused="false"
    >
     {{ text }}
    </vue-marquee>
    </template>
import VueMarquee from 'vue-marquee-text-component';

export default { components: { VueMarquee }, data() { return { text: "这是一段需要滚动显示的文字内容..." } } }

vue实现文字滚动停顿

```

每种方法各有优缺点,CSS动画实现简单但控制不够灵活,JavaScript定时器更可控但代码稍复杂,第三方库最便捷但需要引入额外依赖。根据项目需求选择合适方案。

标签: 停顿文字
分享给朋友:

相关文章

vue实现文字下标

vue实现文字下标

在 Vue 中实现文字下标 Vue 中可以通过多种方式实现文字下标效果,以下是几种常见的方法: 使用 HTML 的 <sub> 标签 HTML 提供了 <sub> 标签用于实…

vue 实现文字折叠

vue 实现文字折叠

Vue 实现文字折叠的方法 使用 v-show 或 v-if 控制显示 通过绑定一个布尔值变量,利用 v-show 或 v-if 动态控制文字的显示与隐藏。v-show 通过 CSS 的 displ…

vue实现图标加文字

vue实现图标加文字

Vue 实现图标加文字的几种方法 在 Vue 中实现图标加文字的常见方式有多种,可以根据项目需求和使用的图标库选择合适的方法。 使用 Element UI 或 Ant Design Vue 等 UI…

使用vue实现文字显示

使用vue实现文字显示

使用 Vue 实现文字显示 Vue.js 提供了多种方式实现文字显示,以下是一些常见的方法: 1. 插值表达式 在 Vue 模板中,可以直接使用双大括号 {{ }} 插入动态文本: <tem…

文字闪现css制作

文字闪现css制作

文字闪现效果的CSS制作方法 文字闪现效果可以通过CSS动画和关键帧实现,以下是几种常见的实现方式: 基础闪现效果 通过opacity属性的变化实现文字淡入淡出: .flash-text {…

js实现文字选择

js实现文字选择

实现文字选择的基本方法 使用JavaScript的window.getSelection()方法可以获取用户选择的文本范围。以下是一个基本示例: function getSelectedText()…