当前位置:首页 > VUE

vue实现左滑动删除

2026-02-21 09:18:10VUE

Vue 实现左滑删除功能

监听触摸事件

在 Vue 组件中监听 touchstarttouchmovetouchend 事件,记录触摸起始位置和移动距离。

data() {
  return {
    startX: 0,
    moveX: 0,
    isDragging: false
  }
},
methods: {
  handleTouchStart(e) {
    this.startX = e.touches[0].clientX
    this.isDragging = false
  },
  handleTouchMove(e) {
    this.moveX = e.touches[0].clientX - this.startX
    this.isDragging = Math.abs(this.moveX) > 10
  },
  handleTouchEnd() {
    if (this.isDragging) {
      // 处理滑动结束逻辑
    }
  }
}

绑定事件到元素

在模板中为需要滑动的元素绑定事件处理器。

<div 
  @touchstart="handleTouchStart"
  @touchmove="handleTouchMove"
  @touchend="handleTouchEnd"
  :style="{ transform: `translateX(${moveX}px)` }"
>
  <!-- 内容 -->
</div>

实现滑动动画

使用 CSS 过渡效果让滑动更流畅,通过 Vue 的响应式数据控制 transform 属性。

vue实现左滑动删除

.slide-item {
  transition: transform 0.3s ease;
}

添加删除按钮

在滑动元素后面放置删除按钮,通过滑动距离控制按钮显示。

<div class="slide-container">
  <div class="slide-content" 
       :style="{ transform: `translateX(${moveX}px)` }">
    <!-- 主要内容 -->
  </div>
  <div class="slide-action" v-show="isDragging && moveX < 0">
    <button @click="handleDelete">删除</button>
  </div>
</div>

处理删除逻辑

在滑动结束时判断滑动距离,如果超过阈值则触发删除操作。

vue实现左滑动删除

handleTouchEnd() {
  if (this.isDragging) {
    if (this.moveX < -100) {
      this.$emit('delete')
    } else {
      this.moveX = 0
    }
    this.isDragging = false
  }
}

优化体验

添加阈值判断和自动回弹效果,提升用户体验。

handleTouchMove(e) {
  const currentX = e.touches[0].clientX
  this.moveX = Math.min(0, Math.max(-100, currentX - this.startX))
  this.isDragging = Math.abs(this.moveX) > 10
}

完整组件示例

<template>
  <div class="slide-container">
    <div 
      class="slide-content"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      :style="{ transform: `translateX(${moveX}px)` }"
    >
      <slot></slot>
    </div>
    <div class="slide-action" v-show="isDragging && moveX < 0">
      <button @click="$emit('delete')">删除</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      moveX: 0,
      isDragging: false
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
      this.isDragging = false
    },
    handleTouchMove(e) {
      const currentX = e.touches[0].clientX
      this.moveX = Math.min(0, Math.max(-100, currentX - this.startX))
      this.isDragging = Math.abs(this.moveX) > 10
    },
    handleTouchEnd() {
      if (this.isDragging) {
        if (this.moveX < -50) {
          this.moveX = -80
        } else {
          this.moveX = 0
        }
        this.isDragging = false
      }
    }
  }
}
</script>

<style>
.slide-container {
  position: relative;
  overflow: hidden;
}
.slide-content {
  transition: transform 0.3s ease;
  z-index: 1;
  background: white;
}
.slide-action {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ff4444;
}
</style>

使用第三方库

对于更复杂的需求,可以考虑使用现成的 Vue 滑动组件库,如 vue-touch-ripplev-touch

npm install vue-touch-ripple
import VueTouchRipple from 'vue-touch-ripple'
Vue.use(VueTouchRipple)

标签: vue
分享给朋友:

相关文章

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…

vue 实现弹窗

vue 实现弹窗

Vue 实现弹窗的方法 在 Vue 中实现弹窗功能可以通过多种方式完成,以下是几种常见的方法: 使用组件和 v-if/v-show 控制显示 创建一个独立的弹窗组件,通过 v-if 或 v-show…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…