当前位置:首页 > VUE

vue实现横向无缝滚动

2026-02-23 04:12:17VUE

实现横向无缝滚动的思路

横向无缝滚动通常用于展示轮播图或横向列表,核心是通过CSS动画或JavaScript动态调整元素位置,实现循环滚动效果。以下是两种常见实现方式:

CSS动画实现

使用CSS的@keyframestransform属性实现动画效果,适合简单场景:

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

<style scoped>
.scroll-container {
  width: 100%;
  overflow: hidden;
}
.scroll-content {
  display: flex;
  animation: scroll 10s linear infinite;
  white-space: nowrap;
}
.item {
  flex-shrink: 0;
  padding: 0 20px;
}
@keyframes scroll {
  0% { transform: translateX(0); }
  100% { transform: translateX(-50%); }
}
</style>

JavaScript动态控制

通过定时器动态修改元素位置,适合需要精确控制的场景:

<template>
  <div ref="container" class="scroll-container" @mouseenter="pause" @mouseleave="resume">
    <div ref="content" class="scroll-content">
      <div v-for="(item, index) in doubledItems" :key="index" class="item">{{ item }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4'],
      speed: 2,
      animationId: null
    }
  },
  computed: {
    doubledItems() {
      return [...this.items, ...this.items] // 克隆一份实现无缝衔接
    }
  },
  mounted() {
    this.startAnimation()
  },
  methods: {
    startAnimation() {
      const container = this.$refs.container
      const content = this.$refs.content
      let position = 0

      const animate = () => {
        position -= this.speed
        if (position <= -content.scrollWidth / 2) {
          position = 0
        }
        content.style.transform = `translateX(${position}px)`
        this.animationId = requestAnimationFrame(animate)
      }
      animate()
    },
    pause() {
      cancelAnimationFrame(this.animationId)
    },
    resume() {
      this.startAnimation()
    }
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  }
}
</script>

<style scoped>
.scroll-container {
  width: 100%;
  overflow: hidden;
}
.scroll-content {
  display: flex;
}
.item {
  flex-shrink: 0;
  padding: 0 20px;
}
</style>

性能优化建议

使用will-change属性提升动画性能:

.scroll-content {
  will-change: transform;
}

考虑使用CSS的scroll-snap-type实现分段滚动:

vue实现横向无缝滚动

.scroll-container {
  scroll-snap-type: x mandatory;
  overflow-x: auto;
}
.item {
  scroll-snap-align: start;
}

注意事项

  • 克隆元素时需确保原始数据不变
  • 移动端需添加触摸事件支持
  • 组件销毁时清除定时器或动画帧
  • 动态内容变化时需要重置滚动位置

标签: 横向vue
分享给朋友:

相关文章

vue computed 实现

vue computed 实现

computed 的基本用法 在 Vue 中,computed 用于声明计算属性,这些属性基于响应式依赖进行缓存。只有当依赖发生变化时,计算属性才会重新计算。 new Vue({ data: {…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕包…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为 @…

vue实现导入

vue实现导入

Vue 实现文件导入功能 使用 <input type="file"> 元素 在 Vue 模板中添加一个文件输入元素,绑定 change 事件处理函数。 <template>…