当前位置:首页 > VUE

vue实现列表左右滑动

2026-01-22 04:41:14VUE

实现列表左右滑动的 Vue 方案

使用 CSS 和 Touch 事件

通过 CSS 的 overflow-x: autowhite-space: nowrap 实现横向滚动容器,结合 Vue 的 @touchstart@touchmove 事件处理滑动逻辑:

<template>
  <div 
    class="scroll-container"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    ref="container"
  >
    <div class="item" v-for="item in items" :key="item.id">
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [...], // 你的列表数据
      startX: 0,
      scrollLeft: 0
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].pageX
      this.scrollLeft = this.$refs.container.scrollLeft
    },
    handleTouchMove(e) {
      const x = e.touches[0].pageX
      const walk = (x - this.startX) * 2 // 调节滑动灵敏度
      this.$refs.container.scrollLeft = this.scrollLeft - walk
    }
  }
}
</script>

<style>
.scroll-container {
  display: flex;
  overflow-x: auto;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch; /* iOS 平滑滚动 */
}
.item {
  flex: 0 0 auto;
  width: 100px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</style>

使用第三方库(Better-Scroll)

Better-Scroll 提供更流畅的滑动体验和额外功能(如惯性滚动、边界回弹):

npm install @better-scroll/core
<template>
  <div ref="wrapper" class="wrapper">
    <div class="content">
      <div v-for="item in items" :key="item.id" class="item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from '@better-scroll/core'

export default {
  data() {
    return {
      items: [...] // 你的列表数据
    }
  },
  mounted() {
    this.bs = new BScroll(this.$refs.wrapper, {
      scrollX: true,
      scrollY: false,
      momentum: true,
      bounce: true
    })
  },
  beforeDestroy() {
    this.bs.destroy()
  }
}
</script>

<style>
.wrapper {
  width: 100%;
  overflow: hidden;
}
.content {
  display: inline-block;
  white-space: nowrap;
}
.item {
  display: inline-block;
  width: 100px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</style>

使用 Swiper 组件

Swiper 是专业的轮播图/滑动库,适合复杂场景:

npm install swiper@6
<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="item in items" :key="item.id">
      {{ item.text }}
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/swiper-bundle.css'

export default {
  components: { Swiper, SwiperSlide },
  data() {
    return {
      items: [...],
      swiperOptions: {
        slidesPerView: 'auto',
        spaceBetween: 10,
        freeMode: true
      }
    }
  }
}
</script>

<style>
.swiper-slide {
  width: auto;
  height: 100px;
  background: #eee;
}
</style>

响应式注意事项

对于需要响应式布局的情况,可以通过监听窗口变化动态调整:

export default {
  // ...
  methods: {
    updateSlider() {
      if (this.bs) {
        this.bs.refresh()
      }
    }
  },
  mounted() {
    window.addEventListener('resize', this.updateSlider)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.updateSlider)
  }
}

性能优化建议

对于长列表,建议使用虚拟滚动技术(如 vue-virtual-scroller)避免渲染大量 DOM 节点:

vue实现列表左右滑动

npm install vue-virtual-scroller
<template>
  <RecycleScroller
    class="scroller"
    :items="items"
    :item-size="100"
    direction="horizontal"
    key-field="id"
  >
    <template v-slot="{ item }">
      <div class="item">
        {{ item.text }}
      </div>
    </template>
  </RecycleScroller>
</template>

标签: 列表vue
分享给朋友:

相关文章

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newIte…