当前位置:首页 > VUE

vue实现左滑删除

2026-02-22 05:49:50VUE

Vue 实现左滑删除功能

使用 touch 事件和 CSS 过渡

监听 touchstarttouchmovetouchend 事件,结合 CSS 过渡效果实现左滑删除。

vue实现左滑删除

<template>
  <div class="list-item" 
       @touchstart="handleTouchStart"
       @touchmove="handleTouchMove"
       @touchend="handleTouchEnd">
    <div class="content">{{ item.text }}</div>
    <div class="delete-btn" @click="handleDelete">删除</div>
  </div>
</template>

<script>
export default {
  props: ['item'],
  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() {
      this.$emit('delete', this.item.id)
    }
  }
}
</script>

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

使用第三方库 vue-swipe-actions

安装 vue-swipe-actions 可以快速实现左滑删除功能。

vue实现左滑删除

npm install vue-swipe-actions
<template>
  <swipe-action>
    <template #left>
      <div class="swipe-content">{{ item.text }}</div>
    </template>
    <template #right>
      <div class="swipe-delete" @click="handleDelete">删除</div>
    </template>
  </swipe-action>
</template>

<script>
import { SwipeAction } from 'vue-swipe-actions'
export default {
  components: { SwipeAction },
  props: ['item'],
  methods: {
    handleDelete() {
      this.$emit('delete', this.item.id)
    }
  }
}
</script>

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

使用 CSS transform 和 transition

通过 CSS 的 transformtransition 属性实现平滑的左滑效果。

<template>
  <div class="swipe-container">
    <div class="swipe-content" :style="{ transform: `translateX(${offsetX}px)` }"
         @touchstart="startDrag"
         @touchmove="onDrag"
         @touchend="endDrag">
      {{ item.text }}
    </div>
    <div class="swipe-action" @click="handleDelete">删除</div>
  </div>
</template>

<script>
export default {
  props: ['item'],
  data() {
    return {
      startX: 0,
      currentX: 0,
      offsetX: 0,
      dragging: false
    }
  },
  methods: {
    startDrag(e) {
      this.dragging = true
      this.startX = e.touches[0].clientX
    },
    onDrag(e) {
      if (!this.dragging) return
      this.currentX = e.touches[0].clientX
      this.offsetX = this.currentX - this.startX
      if (this.offsetX > 0) this.offsetX = 0
    },
    endDrag() {
      this.dragging = false
      if (this.offsetX < -50) {
        this.offsetX = -80
      } else {
        this.offsetX = 0
      }
    },
    handleDelete() {
      this.$emit('delete', this.item.id)
    }
  }
}
</script>

<style>
.swipe-container {
  position: relative;
  overflow: hidden;
  height: 60px;
}
.swipe-content {
  position: absolute;
  top: 0;
  left: 0;
  right: 80px;
  height: 100%;
  background: white;
  transition: transform 0.3s ease;
  padding: 10px;
}
.swipe-action {
  position: absolute;
  top: 0;
  right: 0;
  width: 80px;
  height: 100%;
  background: #ff4949;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

注意事项

  • 移动端开发时需要考虑 touch 事件的兼容性
  • 滑动阈值需要根据实际需求调整
  • 删除操作应该提供确认机制防止误触
  • 列表渲染时需要注意性能优化

标签: vue
分享给朋友:

相关文章

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…

vue实现选人

vue实现选人

实现选人功能的基本思路 在Vue中实现选人功能通常涉及以下核心环节:数据绑定、用户交互处理、状态管理以及界面渲染。以下是具体实现方法: 数据准备与组件结构 创建包含人员信息的数组,通常从API获取或…

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 push…

vue实现队列

vue实现队列

Vue 实现队列功能 在 Vue 中实现队列功能可以通过多种方式完成,以下是几种常见的方法: 使用数组模拟队列 队列遵循先进先出(FIFO)原则,可以用数组的 push 和 shift 方法模拟入队…

vue实现复制

vue实现复制

Vue 实现复制功能 在 Vue 中实现复制功能可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 Clipboard API Clipboard API 是现代浏览器提供的一种原生 AP…