当前位置:首页 > 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 CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue全选实现

vue全选实现

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

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue 实现列表

vue 实现列表

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

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现订单

vue实现订单

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