当前位置:首页 > VUE

vue实现左滑删除

2026-01-21 14:27:50VUE

实现左滑删除的基本思路

在Vue中实现左滑删除功能,通常需要结合触摸事件(touchstart、touchmove、touchend)和CSS过渡效果。通过监听用户的手势滑动,动态调整元素的水平位置,当滑动距离达到阈值时触发删除操作。

监听触摸事件

为需要实现左滑删除的元素绑定触摸事件。通过计算触摸起始点和移动点的差值,确定滑动的方向和距离。

data() {
  return {
    startX: 0,
    moveX: 0,
    translateX: 0
  };
},
methods: {
  handleTouchStart(e) {
    this.startX = e.touches[0].clientX;
  },
  handleTouchMove(e) {
    this.moveX = e.touches[0].clientX;
    this.translateX = this.moveX - this.startX;
    // 限制向右滑动
    if (this.translateX > 0) {
      this.translateX = 0;
    }
  },
  handleTouchEnd() {
    // 滑动距离超过阈值时触发删除
    if (this.translateX < -60) {
      this.deleteItem();
    } else {
      this.translateX = 0;
    }
  },
  deleteItem() {
    // 删除逻辑
  }
}

动态绑定样式

根据计算出的滑动距离,动态绑定元素的transform样式,实现滑动效果。

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

添加过渡效果

为了使滑动更加平滑,可以添加CSS过渡效果。

div {
  transition: transform 0.3s ease;
}

完整组件示例

以下是一个完整的Vue组件示例,实现了左滑删除功能。

<template>
  <div class="list-container">
    <div
      v-for="(item, index) in list"
      :key="index"
      class="list-item"
      @touchstart="handleTouchStart(index, $event)"
      @touchmove="handleTouchMove(index, $event)"
      @touchend="handleTouchEnd(index)"
      :style="{ transform: `translateX(${item.translateX}px)` }"
    >
      <div class="item-content">
        {{ item.text }}
      </div>
      <div class="delete-button" @click="deleteItem(index)">
        删除
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { text: 'Item 1', translateX: 0 },
        { text: 'Item 2', translateX: 0 },
        { text: 'Item 3', translateX: 0 }
      ],
      startX: 0
    };
  },
  methods: {
    handleTouchStart(index, e) {
      this.startX = e.touches[0].clientX;
    },
    handleTouchMove(index, e) {
      const moveX = e.touches[0].clientX;
      this.list[index].translateX = moveX - this.startX;
      if (this.list[index].translateX > 0) {
        this.list[index].translateX = 0;
      }
    },
    handleTouchEnd(index) {
      if (this.list[index].translateX < -60) {
        this.list[index].translateX = -80;
      } else {
        this.list[index].translateX = 0;
      }
    },
    deleteItem(index) {
      this.list.splice(index, 1);
    }
  }
};
</script>

<style>
.list-container {
  width: 100%;
}
.list-item {
  display: flex;
  align-items: center;
  width: 100%;
  height: 60px;
  border-bottom: 1px solid #eee;
  position: relative;
  overflow: hidden;
  transition: transform 0.3s ease;
}
.item-content {
  width: 100%;
  padding: 0 15px;
}
.delete-button {
  position: absolute;
  right: 0;
  width: 80px;
  height: 100%;
  background: #ff4444;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

注意事项

  • 在移动端开发中,需要考虑触摸事件的兼容性。
  • 滑动阈值可以根据实际需求调整。
  • 删除按钮的宽度应与滑动阈值匹配,确保用户滑动到足够距离时按钮完全显示。
  • 为了防止页面滚动与滑动删除冲突,可以在touchmove事件中调用e.preventDefault()

vue实现左滑删除

标签: vue
分享给朋友:

相关文章

vue实现轮播图

vue实现轮播图

Vue 实现轮播图的方法 使用第三方库(推荐) Vue 生态中有许多成熟的轮播图组件库,例如 Swiper、Vue-Awesome-Swiper 等,它们提供了丰富的配置和响应式支持。 安装 vu…

vue实现组件循环

vue实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return {…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件…