当前位置:首页 > 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 slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue实现导入

vue实现导入

Vue 实现文件导入功能 使用 <input type="file"> 元素 在 Vue 模板中添加一个文件输入元素,绑定 change 事件处理函数。 <template>…