当前位置:首页 > VUE

vue实现滑动删除

2026-02-18 04:11:45VUE

实现滑动删除的基本思路

在Vue中实现滑动删除功能通常需要结合触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseup)来检测用户的滑动操作。通过计算滑动距离,动态调整元素的位移,并在滑动超过阈值时触发删除操作。

使用CSS和事件监听实现

创建一个可滑动的列表项组件,通过transform属性实现滑动效果,结合事件监听判断滑动距离。

<template>
  <div class="list-container">
    <div 
      v-for="(item, index) in items" 
      :key="index"
      class="list-item"
      @touchstart="startSwipe($event, index)"
      @touchmove="moveSwipe($event, index)"
      @touchend="endSwipe(index)"
      @mousedown="startSwipe($event, index)"
      @mousemove="moveSwipe($event, index)"
      @mouseup="endSwipe(index)"
    >
      <div class="item-content">{{ item.text }}</div>
      <div class="delete-button" @click="deleteItem(index)">删除</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { text: '项目1', offsetX: 0 },
        { text: '项目2', offsetX: 0 },
        { text: '项目3', offsetX: 0 }
      ],
      startX: 0,
      isSwiping: false
    }
  },
  methods: {
    startSwipe(e, index) {
      this.startX = e.type.includes('mouse') ? e.clientX : e.touches[0].clientX
      this.isSwiping = true
    },
    moveSwipe(e, index) {
      if (!this.isSwiping) return
      const currentX = e.type.includes('mouse') ? e.clientX : e.touches[0].clientX
      const diffX = currentX - this.startX
      if (diffX < 0) {
        this.items[index].offsetX = Math.max(diffX, -100)
      }
      e.preventDefault()
    },
    endSwipe(index) {
      if (this.items[index].offsetX < -50) {
        this.items[index].offsetX = -80
      } else {
        this.items[index].offsetX = 0
      }
      this.isSwiping = false
    },
    deleteItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

<style>
.list-container {
  width: 100%;
  overflow: hidden;
}
.list-item {
  position: relative;
  height: 60px;
  margin: 10px 0;
  transition: transform 0.3s ease;
}
.item-content {
  position: absolute;
  left: 0;
  right: 0;
  height: 100%;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  padding: 0 15px;
  display: flex;
  align-items: center;
  z-index: 1;
  transform: translateX(v-bind('item.offsetX + "px"'));
}
.delete-button {
  position: absolute;
  right: 0;
  width: 80px;
  height: 100%;
  background: #ff5252;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 0 8px 8px 0;
  cursor: pointer;
}
</style>

使用第三方库实现

对于更复杂的滑动删除需求,可以使用现成的Vue组件库,如vant、vuesax等,它们提供了内置的滑动单元格组件。

安装vant:

npm install vant

使用vant的SwipeCell组件:

<template>
  <van-swipe-cell v-for="(item, index) in items" :key="index">
    <template #left>
      <van-button square type="primary" text="选择" />
    </template>
    <van-cell :title="item.text" />
    <template #right>
      <van-button 
        square 
        type="danger" 
        text="删除" 
        @click="deleteItem(index)" 
      />
    </template>
  </van-swipe-cell>
</template>

<script>
import { SwipeCell, Button, Cell } from 'vant'
export default {
  components: {
    [SwipeCell.name]: SwipeCell,
    [Button.name]: Button,
    [Cell.name]: Cell
  },
  data() {
    return {
      items: [
        { text: '项目1' },
        { text: '项目2' },
        { text: '项目3' }
      ]
    }
  },
  methods: {
    deleteItem(index) {
      this.items.splice(index, 1)
    }
  }
}
</script>

性能优化建议

对于长列表,滑动删除可能会引起性能问题。建议使用虚拟滚动技术(如vue-virtual-scroller)来优化性能。

安装vue-virtual-scroller:

npm install vue-virtual-scroller

结合虚拟滚动实现:

<template>
  <RecycleScroller
    class="scroller"
    :items="items"
    :item-size="80"
    key-field="id"
    v-slot="{ item, index }"
  >
    <div class="list-item">
      <!-- 滑动删除内容 -->
    </div>
  </RecycleScroller>
</template>

注意事项

移动端和PC端的事件处理需要兼容,示例代码中已经同时处理了touch和mouse事件。滑动阈值(如-50px)可以根据实际需求调整。删除操作建议添加确认提示,防止误操作。

vue实现滑动删除

标签: vue
分享给朋友:

相关文章

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload(…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

proxy实现vue

proxy实现vue

使用 Proxy 实现 Vue 响应式系统 Vue 3 的响应式系统基于 JavaScript 的 Proxy API,通过代理对象实现对属性的拦截和依赖追踪。以下是实现的核心逻辑: 创建响应式…