您好,登录后才能下订单哦!
在移动端开发中,右滑删除是一个非常常见的交互方式,尤其是在聊天列表、任务列表等场景中。本文将详细介绍如何在Vue3中实现一个简易的微信右滑删除逻辑。
我们需要实现一个类似于微信聊天列表的右滑删除功能,具体需求如下:
为了实现这个功能,我们需要使用以下技术:
首先,我们需要创建一个Vue3项目。如果你还没有安装Vue CLI,可以通过以下命令安装:
npm install -g @vue/cli
然后,创建一个新的Vue3项目:
vue create vue3-swipe-delete
接下来,我们创建一个简单的列表组件 SwipeList.vue
,用于展示列表项。
<template>
<div class="swipe-list">
<div
v-for="(item, index) in items"
:key="item.id"
class="swipe-item"
@touchstart="onTouchStart(index, $event)"
@touchmove="onTouchMove(index, $event)"
@touchend="onTouchEnd(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: [
{ id: 1, text: '列表项 1' },
{ id: 2, text: '列表项 2' },
{ id: 3, text: '列表项 3' },
],
startX: 0,
currentX: 0,
isSwiping: false,
};
},
methods: {
onTouchStart(index, event) {
this.startX = event.touches[0].clientX;
this.currentX = this.startX;
this.isSwiping = true;
},
onTouchMove(index, event) {
if (!this.isSwiping) return;
this.currentX = event.touches[0].clientX;
const deltaX = this.currentX - this.startX;
if (deltaX < 0) {
this.items[index].offsetX = Math.max(deltaX, -100); // 限制最大滑动距离
}
},
onTouchEnd(index) {
this.isSwiping = false;
const deltaX = this.currentX - this.startX;
if (deltaX < -50) {
this.items[index].offsetX = -100; // 自动展开删除按钮
} else {
this.items[index].offsetX = 0; // 恢复原位
}
},
deleteItem(index) {
this.items.splice(index, 1);
},
},
};
</script>
<style scoped>
.swipe-list {
width: 100%;
overflow: hidden;
}
.swipe-item {
position: relative;
width: 100%;
height: 60px;
display: flex;
align-items: center;
background-color: #fff;
border-bottom: 1px solid #eee;
transition: transform 0.3s ease;
}
.item-content {
flex: 1;
padding: 0 16px;
}
.delete-button {
width: 100px;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: #ff4444;
color: #fff;
transform: translateX(100%);
transition: transform 0.3s ease;
}
.swipe-item[style*="translateX"] .delete-button {
transform: translateX(0);
}
</style>
在 SwipeList.vue
组件中,我们通过 touchstart
、touchmove
和 touchend
事件来监听用户的滑动操作。
在 deleteItem
方法中,我们通过 splice
方法删除对应的列表项。
通过CSS3的 transform
属性来实现滑动动画,并通过 transition
属性来平滑过渡。
以下是完整的 SwipeList.vue
组件代码:
<template>
<div class="swipe-list">
<div
v-for="(item, index) in items"
:key="item.id"
class="swipe-item"
:style="{ transform: `translateX(${item.offsetX || 0}px)` }"
@touchstart="onTouchStart(index, $event)"
@touchmove="onTouchMove(index, $event)"
@touchend="onTouchEnd(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: [
{ id: 1, text: '列表项 1' },
{ id: 2, text: '列表项 2' },
{ id: 3, text: '列表项 3' },
],
startX: 0,
currentX: 0,
isSwiping: false,
};
},
methods: {
onTouchStart(index, event) {
this.startX = event.touches[0].clientX;
this.currentX = this.startX;
this.isSwiping = true;
},
onTouchMove(index, event) {
if (!this.isSwiping) return;
this.currentX = event.touches[0].clientX;
const deltaX = this.currentX - this.startX;
if (deltaX < 0) {
this.items[index].offsetX = Math.max(deltaX, -100); // 限制最大滑动距离
}
},
onTouchEnd(index) {
this.isSwiping = false;
const deltaX = this.currentX - this.startX;
if (deltaX < -50) {
this.items[index].offsetX = -100; // 自动展开删除按钮
} else {
this.items[index].offsetX = 0; // 恢复原位
}
},
deleteItem(index) {
this.items.splice(index, 1);
},
},
};
</script>
<style scoped>
.swipe-list {
width: 100%;
overflow: hidden;
}
.swipe-item {
position: relative;
width: 100%;
height: 60px;
display: flex;
align-items: center;
background-color: #fff;
border-bottom: 1px solid #eee;
transition: transform 0.3s ease;
}
.item-content {
flex: 1;
padding: 0 16px;
}
.delete-button {
width: 100px;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: #ff4444;
color: #fff;
transform: translateX(100%);
transition: transform 0.3s ease;
}
.swipe-item[style*="translateX"] .delete-button {
transform: translateX(0);
}
</style>
在项目根目录下运行以下命令,启动开发服务器:
npm run serve
打开浏览器,访问 http://localhost:8080
,即可看到效果。
通过本文的介绍,我们实现了一个简易的微信右滑删除功能。这个功能的核心在于监听用户的滑动操作,并通过CSS3的 transform
属性来实现滑动动画。在实际开发中,你可以根据需求进一步优化和扩展这个功能,例如添加更多的交互效果、支持多手势操作等。
希望本文对你有所帮助,祝你在Vue3开发中取得更多成果!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。