当前位置:首页 > VUE

vue实现左右滑动列表

2026-02-21 10:25:30VUE

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

使用 CSS 和 touch 事件

通过 CSS 的 overflow-xwhite-space 属性实现横向滚动,结合 Vue 的 @touchstart@touchmove@touchend 事件处理滑动逻辑。

<template>
  <div 
    class="scroll-container"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div class="scroll-content" :style="{ transform: `translateX(${offsetX}px)` }">
      <div v-for="item in items" :key="item.id" class="item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [...], // 列表数据
      startX: 0,
      offsetX: 0,
      isDragging: false
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
      this.isDragging = true
    },
    handleTouchMove(e) {
      if (!this.isDragging) return
      const currentX = e.touches[0].clientX
      this.offsetX += currentX - this.startX
      this.startX = currentX
    },
    handleTouchEnd() {
      this.isDragging = false
      // 可以添加边界检测和惯性滑动逻辑
    }
  }
}
</script>

<style>
.scroll-container {
  width: 100%;
  overflow-x: hidden;
  touch-action: pan-y;
}
.scroll-content {
  display: flex;
  white-space: nowrap;
  transition: transform 0.3s ease;
}
.item {
  flex: 0 0 auto;
  width: 100px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</style>

使用第三方库(如 vue-swipe)

安装 vue-swipe 库可以快速实现滑动列表功能。

npm install vue-swipe --save
<template>
  <swipe class="my-swipe">
    <swipe-item v-for="item in items" :key="item.id">
      {{ item.text }}
    </swipe-item>
  </swipe>
</template>

<script>
import { Swipe, SwipeItem } from 'vue-swipe'

export default {
  components: {
    Swipe,
    SwipeItem
  },
  data() {
    return {
      items: [...] // 列表数据
    }
  }
}
</script>

<style>
.my-swipe {
  height: 100px;
}
</style>

使用 CSS Scroll Snap

CSS Scroll Snap 提供原生滚动对齐功能,适合实现分页滑动效果。

<template>
  <div class="scroll-container">
    <div class="scroll-content">
      <div v-for="item in items" :key="item.id" class="item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<style>
.scroll-container {
  width: 100%;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}
.scroll-content {
  display: flex;
  width: max-content;
}
.item {
  scroll-snap-align: start;
  width: 100vw;
  height: 100px;
  flex: 0 0 auto;
}
</style>

以上方法可根据项目需求选择,CSS 方案适合简单需求,第三方库提供更丰富的功能,CSS Scroll Snap 则利用浏览器原生特性实现流畅效果。

vue实现左右滑动列表

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

相关文章

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-mode…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Route…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…