当前位置:首页 > VUE

vue 左滑实现删除

2026-01-22 04:15:59VUE

Vue 左滑删除实现方法

使用 touch 事件监听滑动

在 Vue 中监听 touchstart、touchmove 和 touchend 事件,计算滑动距离来判断是否触发删除操作。通过动态绑定样式实现滑动效果。

<template>
  <div class="list-item" 
       @touchstart="handleTouchStart"
       @touchmove="handleTouchMove"
       @touchend="handleTouchEnd">
    <div class="content">滑动删除我</div>
    <div class="delete-btn" @click="handleDelete">删除</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      moveX: 0,
      offsetX: 0
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
    },
    handleTouchMove(e) {
      this.moveX = e.touches[0].clientX
      this.offsetX = this.moveX - this.startX
      if (this.offsetX < 0) {
        e.currentTarget.style.transform = `translateX(${this.offsetX}px)`
      }
    },
    handleTouchEnd(e) {
      if (this.offsetX < -50) {
        e.currentTarget.style.transform = 'translateX(-80px)'
      } else {
        e.currentTarget.style.transform = 'translateX(0)'
      }
    },
    handleDelete() {
      // 删除逻辑
    }
  }
}
</script>

<style>
.list-item {
  display: flex;
  position: relative;
  overflow: hidden;
  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

vue-swipe-actions 是专门为 Vue 实现的滑动操作组件,简化了实现过程。

vue  左滑实现删除

安装依赖:

vue  左滑实现删除

npm install vue-swipe-actions

使用示例:

<template>
  <swipe-action>
    <template #left>
      <div class="content">可左滑删除的内容</div>
    </template>
    <template #right>
      <div class="delete" @click="deleteItem">删除</div>
    </template>
  </swipe-action>
</template>

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

export default {
  components: {
    SwipeAction
  },
  methods: {
    deleteItem() {
      // 删除逻辑
    }
  }
}
</script>

<style>
.content {
  padding: 15px;
  background: #fff;
}
.delete {
  width: 80px;
  background: red;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

使用 CSS transform 和 transition

纯 CSS 方案结合 Vue 的数据绑定实现滑动效果,通过修改数据状态控制元素位置。

<template>
  <div class="swipe-container">
    <div class="swipe-content" :style="{ transform: `translateX(${offsetX}px)` }"
         @touchstart="startSwipe"
         @touchmove="moveSwipe"
         @touchend="endSwipe">
      <div class="item-content">滑动删除内容</div>
      <div class="delete-area" @click="confirmDelete">删除</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      currentX: 0,
      offsetX: 0,
      isSwiping: false
    }
  },
  methods: {
    startSwipe(e) {
      this.startX = e.touches[0].clientX
      this.isSwiping = true
    },
    moveSwipe(e) {
      if (!this.isSwiping) return
      this.currentX = e.touches[0].clientX
      this.offsetX = Math.min(0, this.currentX - this.startX)
    },
    endSwipe() {
      this.isSwiping = false
      if (this.offsetX < -50) {
        this.offsetX = -80
      } else {
        this.offsetX = 0
      }
    },
    confirmDelete() {
      // 删除逻辑
    }
  }
}
</script>

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

移动端优化注意事项

  • 防止页面滚动与滑动删除冲突,可在 touchmove 事件中调用 e.preventDefault()
  • 设置合适的滑动阈值,通常 50px 左右触发删除操作
  • 添加过渡动画使交互更流畅
  • 考虑添加防抖处理避免频繁触发
  • 在 PC 端可考虑添加鼠标事件支持

标签: vue
分享给朋友:

相关文章

vue 表格实现

vue 表格实现

Vue 表格实现方法 在 Vue 中实现表格功能可以通过多种方式完成,以下介绍几种常见的方法: 使用原生 HTML 表格 最简单的表格可以通过 Vue 模板中的原生 HTML 表格元素实现:…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vu…

vue实现递归

vue实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据(如菜单、评论列表、文件目录等)。以下是具体实现方法: 定义递归组件 通过 name 选项让组件可以递归调用自身:…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…