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

vue实现tab换行

Vue实现Tab切换的常见方法 使用Vue实现Tab切换功能可以通过多种方式完成,以下是几种常见的方法: 动态组件结合v-if或v-show 通过v-if或v-show控制不同Tab内容的显示与隐…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue实现tabs

vue实现tabs

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