当前位置:首页 > VUE

vue实现向右无缝滚动

2026-01-22 00:57:39VUE

Vue 实现向右无缝滚动

使用 CSS 动画和 Vue 数据绑定

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

vue实现向右无缝滚动

<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 动态控制滚动位置,实现更灵活的无缝滚动效果。

vue实现向右无缝滚动

<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 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue播放倍速怎么实现

vue播放倍速怎么实现

实现Vue播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取v…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现递归

vue实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据(如菜单、评论列表、文件目录等)。以下是具体实现方法: 定义递归组件 通过 name 选项让组件可以递归调用自身:…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端…