当前位置:首页 > 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 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 con…