当前位置:首页 > 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>

无缝循环滚动多条内容

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

vue实现向右无缝滚动

<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实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…