当前位置:首页 > VUE

vue实现文本滚动

2026-02-20 14:04:15VUE

Vue 实现文本滚动的方法

使用 CSS 动画实现

通过 CSS 的 animation@keyframes 实现文本滚动效果。适用于简单的单行文本滚动。

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

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

<style>
.scroll-container {
  width: 200px;
  overflow: hidden;
  white-space: nowrap;
}

.scroll-text {
  display: inline-block;
  animation: scroll 10s linear infinite;
}

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

使用 Vue 指令实现

自定义 Vue 指令实现更灵活的文本滚动控制,支持暂停和继续。

<template>
  <div v-scroll-text="options">{{ text }}</div>
</template>

<script>
export default {
  data() {
    return {
      text: "自定义指令实现的文本滚动效果,支持动态调整速度和方向。",
      options: {
        speed: 50, // 像素/秒
        direction: 'left', // left, right, up, down
        pauseOnHover: true
      }
    };
  },
  directives: {
    scrollText: {
      inserted(el, binding) {
        const options = binding.value || {};
        const speed = options.speed || 50;
        const direction = options.direction || 'left';
        const pauseOnHover = options.pauseOnHover !== false;

        let animationId;
        let position = 0;
        const containerWidth = el.offsetWidth;
        const textWidth = el.scrollWidth;

        const animate = () => {
          if (direction === 'left') {
            position -= 1;
            if (-position >= textWidth) position = containerWidth;
          } else if (direction === 'right') {
            position += 1;
            if (position >= containerWidth) position = -textWidth;
          }
          el.style.transform = `translateX(${position}px)`;
          animationId = requestAnimationFrame(animate);
        };

        const startAnimation = () => {
          if (!animationId) {
            animationId = requestAnimationFrame(animate);
          }
        };

        const stopAnimation = () => {
          if (animationId) {
            cancelAnimationFrame(animationId);
            animationId = null;
          }
        };

        startAnimation();

        if (pauseOnHover) {
          el.addEventListener('mouseenter', stopAnimation);
          el.addEventListener('mouseleave', startAnimation);
        }

        el._scrollAnimation = {
          start: startAnimation,
          stop: stopAnimation,
          cleanup: () => {
            stopAnimation();
            if (pauseOnHover) {
              el.removeEventListener('mouseenter', stopAnimation);
              el.removeEventListener('mouseleave', startAnimation);
            }
          }
        };
      },
      unbind(el) {
        if (el._scrollAnimation) {
          el._scrollAnimation.cleanup();
        }
      }
    }
  }
};
</script>

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

对于更复杂的需求,可以使用专门为 Vue 开发的跑马灯文本组件。

安装依赖:

npm install vue-marquee-text-component

使用示例:

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

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

export default {
  components: {
    MarqueeText
  },
  data() {
    return {
      text: "使用第三方库实现的文本滚动效果,支持多种配置选项。"
    };
  }
};
</script>

实现垂直滚动列表

对于需要垂直滚动的列表内容,可以使用 CSS 动画实现。

<template>
  <div class="vertical-scroll-container">
    <ul class="scroll-list">
      <li v-for="(item, index) in items" :key="index">{{ item }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        "第一条滚动内容",
        "第二条滚动内容",
        "第三条滚动内容",
        "第四条滚动内容"
      ]
    };
  },
  mounted() {
    this.startScroll();
  },
  methods: {
    startScroll() {
      const container = this.$el.querySelector('.scroll-list');
      const firstItem = container.firstElementChild;

      setInterval(() => {
        container.style.transition = 'transform 0.5s ease';
        container.style.transform = 'translateY(-100%)';

        setTimeout(() => {
          container.style.transition = 'none';
          container.appendChild(firstItem);
          container.style.transform = 'translateY(0)';
        }, 500);
      }, 2000);
    }
  }
};
</script>

<style>
.vertical-scroll-container {
  height: 100px;
  overflow: hidden;
}

.scroll-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
</style>

每种方法适用于不同场景,可根据项目需求选择最合适的实现方式。CSS 动画适合简单效果,自定义指令提供更多控制,第三方库节省开发时间,垂直滚动适用于新闻列表等场景。

vue实现文本滚动

标签: 文本vue
分享给朋友:

相关文章

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

实现 vue ssr

实现 vue ssr

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

vue实现建模

vue实现建模

Vue 实现建模的方法 在 Vue 中实现建模通常涉及数据绑定、组件化和状态管理。以下是几种常见的方法: 数据驱动建模 Vue 的核心是数据驱动视图。通过定义数据模型,Vue 会自动更新 DOM。…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…