当前位置: 首页 > news >正文

uniapp实现像qq消息列表左滑显示右侧操作栏效果

先看效果图

代码

SlidableChatEntry.vue

<template><!-- 聊天项列表 --><view class="chat-item"@touchstart="handleTouchStart($event)"@touchmove="handleTouchMove($event)"@touchend="handleTouchEnd()"><!-- 聊天内容区域 --><view class="chat-content":style="getContentStyle()"><slot name="content"></slot></view><!-- 操作按钮区域 --><view class="action-buttons":style="getButtonStyle()"@click.stop="handleAction()"><slot name="menu"></slot></view></view>
</template>
<script>
export default {data() {return {chatList: [],// 记录每个聊天项的滑动状态swipeStates: [], // 存储每个聊天项的滑动距离touchStartXs: [] // 记录每个聊天项的触摸起始位置}},created() {// 初始化数组this.chatList.forEach(() => {this.swipeStates.push()this.touchStartXs.push()})},methods: {// 触摸开始事件handleTouchStart(e) {this.touchStartXs = e.touches[0].clientX},// 触摸移动事件handleTouchMove(e, index) {const touchX = e.touches[0].clientXlet deltaX = touchX - this.touchStartXs// 限制只能向左滑动(禁止向右滑动超过初始位置)if (deltaX > 0) {deltaX = 0}// 限制最大滑动距离为按钮总宽度(约150px)if (deltaX < -150) {deltaX = -150}this.swipeStates = deltaX},// 触摸结束事件handleTouchEnd() {// 如果滑动距离超过阈值(50px),则完全滑出按钮if (this.swipeStates < -50) {this.swipeStates = -150 // 完全滑出} else {// 否则恢复原状this.swipeStates = 0}},//恢复原状closeSwipe(){this.swipeStates = 0},// 获取内容区域样式getContentStyle() {return {transform: `translateX(${this.swipeStates}px)`}},// 获取按钮区域样式getButtonStyle() {return {transform: `translateX(${this.swipeStates + 150}px)` // 初始位置在屏幕外(150px)}},// 处理操作按钮点击handleAction() {}}
}
</script>
<style scoped>.chat-item {position: relative;overflow: hidden; /* 防止按钮溢出 */box-sizing: border-box;
}/* 聊天内容区域 */
.chat-content {display: flex;align-items: center;flex: 1;z-index: 2; /* 确保内容在按钮上方 */transition: transform 0.3s ease;
}/* 操作按钮区域 */
.action-buttons {position: absolute;right: 0;top: 0;bottom: 0;z-index: 1; /* 按钮在内容下方 */transition: transform 0.3s ease;
}
</style>

使用

 组件引用

<SlidableChatEntry><template v-slot:content>//在这里写你的具体内容</template><template v-slot:menu>//在这里写你的按钮</template>
</SlidableChatEntry >

组件导入

import SlidableChatEntry from '@/components/SlidableChatEntry.vue'
export default {components: {SlidableChatEntry }
}

 样式完全自己掌控,此组件套上去不会改变被套元素的原有样式

相关文章:

  • Qt—(Qt线程,Qt进程,,QT与sqlite数据库)
  • 学习华为 ensp 的学习心得体会
  • SP-VLA:一种用于 VLA 模型加速的联合模型调度和 token 剪枝方法
  • 力扣-136.只出现一次的数字
  • C语言:二分搜索函数
  • linux中的数据检索
  • mysql查询使用`_rowid` 虚拟列
  • iOS APP上架App Store实践:通过自动化流程和辅助工具高效提
  • 【Python】.pyz:源码与依赖打包
  • IPv6 | 地址解析 / 地址管理 / 邻居发现协议(NDP)/ 无状态自动配置(SLAAC)
  • Spring Boot自动配置原理
  • Spring Boot + MyBatis + Vue:全栈开发中的最佳实践
  • ASP3605芯片在煤炭设备电源管理中的可靠性设计与应用探索
  • mapbox进阶,mapbox-gl-draw绘图插件扩展,编辑支持右键取消节点
  • SAP调用api
  • 创客匠人服务体系解析:知识 IP 变现的全链路赋能模型
  • 【网工】华为配置专题进阶篇③
  • FPGA--hello
  • MySQL慢SQL优化全攻略:从诊断到调优
  • C++ 学习笔记精要(二)
  • 西安网站建设ipv6/东莞营销网站建设直播
  • 多屏合一网站建设/郑州网络营销哪家正规
  • 营销型和展示型网站的区别/谷歌google官网下载
  • 网站编辑合适内向的人做吗/海外新闻发布
  • 为网站开发app/整合营销包括哪三方面
  • 大连网站建设招聘网/微博推广价格表