当前位置:首页 > 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 提供更流畅的滑动体验和额外功能(如惯性滚动、边界回弹):

vue实现列表左右滑动

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 是专业的轮播图/滑动库,适合复杂场景:

vue实现列表左右滑动

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 节点:

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全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue登录实现

vue登录实现

Vue 登录实现 实现登录功能通常需要前端与后端配合,Vue 作为前端框架,主要负责表单处理、请求发送和状态管理。以下是常见的实现方式: 表单设计与数据绑定 使用 Vue 的 v-model 实现表…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…

vue遮罩实现

vue遮罩实现

实现 Vue 遮罩的常见方法 使用 CSS 定位和透明度 创建一个全屏遮罩层,通过 CSS 固定定位覆盖整个视口,并设置半透明背景色。 <template> <div cl…