当前位置:首页 > VUE

vue实现向右无缝滚动

2026-01-22 00:57:39VUE

Vue 实现向右无缝滚动

使用 CSS 动画和 Vue 数据绑定

通过 CSS 的 animation 属性和 Vue 的数据绑定实现无缝滚动效果。创建一个包含滚动内容的容器,利用 CSS 动画让内容从右向左移动。

<template>
  <div class="scroll-container">
    <div class="scroll-content" :style="{ animation: `scroll ${duration}s linear infinite` }">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: "这里是需要滚动的内容,可以是任意文本或HTML元素。",
      duration: 10 // 动画持续时间,单位秒
    };
  }
};
</script>

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

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

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

使用 JavaScript 动态控制滚动

通过 Vue 的 ref 和 JavaScript 的 setInterval 动态控制滚动位置,实现更灵活的无缝滚动效果。

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

<script>
export default {
  data() {
    return {
      content: "这里是需要滚动的内容,可以是任意文本或HTML元素。",
      speed: 2 // 滚动速度,单位像素/帧
    };
  },
  mounted() {
    this.startScrolling();
  },
  methods: {
    startScrolling() {
      const container = this.$refs.container;
      const content = this.$refs.content;
      let position = 0;

      setInterval(() => {
        position -= this.speed;
        if (position <= -content.offsetWidth) {
          position = container.offsetWidth;
        }
        content.style.transform = `translateX(${position}px)`;
      }, 16); // 约60帧/秒
    }
  }
};
</script>

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

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

动态内容无缝滚动

如果内容需要动态更新,可以通过 Vue 的 watchcomputed 属性监听内容变化,并重新计算滚动逻辑。

<template>
  <div class="scroll-container" ref="container">
    <div class="scroll-content" ref="content">
      {{ dynamicContent }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dynamicContent: "",
      speed: 2
    };
  },
  mounted() {
    this.startScrolling();
    // 模拟动态内容更新
    setInterval(() => {
      this.dynamicContent = new Date().toLocaleString();
    }, 1000);
  },
  methods: {
    startScrolling() {
      const container = this.$refs.container;
      const content = this.$refs.content;
      let position = 0;

      setInterval(() => {
        position -= this.speed;
        if (position <= -content.offsetWidth) {
          position = container.offsetWidth;
        }
        content.style.transform = `translateX(${position}px)`;
      }, 16);
    }
  }
};
</script>

无缝循环滚动多条内容

对于多条内容需要循环滚动,可以通过复制内容并动态调整位置实现无缝效果。

<template>
  <div class="scroll-container" ref="container">
    <div class="scroll-content" ref="content">
      <span v-for="(item, index) in items" :key="index">{{ item }}</span>
      <span v-for="(item, index) in items" :key="index + items.length">{{ item }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ["内容1", "内容2", "内容3"],
      speed: 2
    };
  },
  mounted() {
    this.startScrolling();
  },
  methods: {
    startScrolling() {
      const container = this.$refs.container;
      const content = this.$refs.content;
      let position = 0;
      const contentWidth = content.offsetWidth / 2;

      setInterval(() => {
        position -= this.speed;
        if (position <= -contentWidth) {
          position = 0;
        }
        content.style.transform = `translateX(${position}px)`;
      }, 16);
    }
  }
};
</script>

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

.scroll-content {
  display: inline-block;
}

.scroll-content span {
  margin-right: 20px;
}
</style>

注意事项

  • 确保滚动内容的宽度足够,否则可能无法看到滚动效果。
  • 调整 speed 和动画持续时间 (duration) 可以改变滚动速度。
  • 对于动态内容,注意及时清理定时器以避免内存泄漏。

vue实现向右无缝滚动

标签: vue
分享给朋友:

相关文章

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Reflec…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const Bas…

vue中实现显示和隐藏

vue中实现显示和隐藏

控制显示与隐藏的方法 在Vue中实现元素的显示和隐藏可以通过多种方式完成,以下是常见的几种方法: 1. 使用v-if指令v-if根据条件动态添加或移除DOM元素,适合需要频繁切换的场景。当条件为fa…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <temp…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-s…

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted 和…