当前位置:首页 > 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 Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <templ…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要…