当前位置:首页 > VUE

vue实现滑动删除

2026-03-30 04:47:37VUE

Vue 实现滑动删除功能

滑动删除是移动端常见的交互方式,可以通过 Vue 结合手势事件或第三方库实现。以下是两种常见的实现方法:

使用 Touch 事件原生实现

通过监听 touchstarttouchmovetouchend 事件计算滑动距离,动态调整元素位置。

<template>
  <div class="list-container">
    <div 
      v-for="(item, index) in list" 
      :key="index"
      class="list-item"
      @touchstart="handleTouchStart($event, index)"
      @touchmove="handleTouchMove($event, index)"
      @touchend="handleTouchEnd(index)"
      :style="{ transform: `translateX(${item.offsetX}px)` }"
    >
      <div class="content">{{ item.text }}</div>
      <div class="delete-btn" @click="deleteItem(index)">删除</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { text: 'Item 1', offsetX: 0 },
        { text: 'Item 2', offsetX: 0 }
      ],
      startX: 0
    }
  },
  methods: {
    handleTouchStart(e, index) {
      this.startX = e.touches[0].clientX
    },
    handleTouchMove(e, index) {
      const currentX = e.touches[0].clientX
      const offsetX = currentX - this.startX
      if (offsetX < 0) {
        this.list[index].offsetX = Math.max(offsetX, -80) // 限制最大滑动距离
      }
    },
    handleTouchEnd(index) {
      if (this.list[index].offsetX < -40) { // 滑动超过阈值保持打开
        this.list[index].offsetX = -80
      } else { // 否则回弹
        this.list[index].offsetX = 0
      }
    },
    deleteItem(index) {
      this.list.splice(index, 1)
    }
  }
}
</script>

<style>
.list-container {
  overflow: hidden;
}
.list-item {
  display: flex;
  position: relative;
  transition: transform 0.3s ease;
}
.content {
  flex: 1;
  padding: 15px;
  background: #fff;
}
.delete-btn {
  width: 80px;
  background: red;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

使用 vue-swipe-actions 库

对于更复杂的滑动交互,可以使用专门的开源库简化实现。

安装库:

npm install vue-swipe-actions

基本用法:

vue实现滑动删除

<template>
  <swipe-action>
    <template #left>
      <swipe-action-item @click="onLeftClick(item)">
        收藏
      </swipe-action-item>
    </template>

    <div class="content">
      {{ item.content }}
    </div>

    <template #right>
      <swipe-action-item @click="onDelete(item)" color="red">
        删除
      </swipe-action-item>
    </template>
  </swipe-action>
</template>

<script>
import { SwipeAction, SwipeActionItem } from 'vue-swipe-actions'
import 'vue-swipe-actions/dist/vue-swipe-actions.css'

export default {
  components: {
    SwipeAction,
    SwipeActionItem
  },
  methods: {
    onDelete(item) {
      // 删除逻辑
    },
    onLeftClick(item) {
      // 其他操作
    }
  }
}
</script>

实现要点

  1. 手势检测需要处理 touch 事件,注意移动端兼容性
  2. 滑动距离应有限制,避免过度滑动
  3. 添加过渡动画提升用户体验
  4. 考虑滑动冲突问题,可能需要阻止页面滚动
  5. 删除操作应有确认机制,防止误触

两种方式各有优劣:原生实现更轻量但需要处理更多细节;使用库更方便但会增加包体积。根据项目需求选择合适方案。

标签: vue
分享给朋友:

相关文章

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript 图表…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Comp…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export default…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…