【uni-app】点击显示和隐藏操作按钮

本文介绍如何在uni-app中通过点击事件控制操作按钮的显示与隐藏。当点击某一项目时,该项目的操作列表显示,同时隐藏其他项的操作。再次点击其他项或者点击最外层时,所有操作都会隐藏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果说明:点击当前项显示操作列表,点击其他项先隐藏其他操作在显示当前的操作。点击最外层隐藏所有操作。
在这里插入图片描述

<template>
	<view class="wrap" @click="hideIsShow">
		<view class="list">
			<view class="item flex justify-between" v-for="(item, index) in list" :key="index">
				<view class="left">
					<view class="top flex">
						<view class="name">{{ item.name }}</view>
						<view class="state">
							<text :class="[item.state == 1 ? 'stateTrue' : 'stateFlase']">{{item.state == 0 ? '未绑定微信' :'已绑定'}}</text>
						</view>
					</view>
					<view class="desc">
						<text class="text">联系电话</text>
						{{ item.tell }}
					</view>
					<view class="desc">
						<text class="text">微信名称</text>
						{{ item.nickName }}
					</view>
				</view>
				<view class="right flex flex-column justify-between">
					<view class="operation">
						<image class="img" src="../../../../static/image/dots.png" mode="aspectFill" @click.stop="operations(index)"></image>
						
						<view class="btns" :class="[isShow == index ? 'show' : 'hide']">
							<view class="flex justify-between align-items-center">
								<text class="text" @click="del(index)">删除</text>
								<text class="text" @click="change(index)">换绑</text>
								<text class="text" @click="edit(index)">编辑</text>
							</view>
						</view>
					</view>
					
					<view class="bweixin" @click="bweixin(e)" :class="[item.state == 1 ? 'hide' : 'show']">绑定微信</view>
				</view>
			</view>
		</view>
		
		<view class="addBtn">
			<image src="../../../../static/image/addComment.png" mode="aspectFill" class="img"></image> 添加店长
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			isShow:-1,
			list: [
				{
					name: '张三',
					state: 1, //0未绑定微信 1已绑定
					tell: '15089980356',
					nickName: 'KK'
				},
				{
					name: '李强',
					state: 0, //0未绑定微信 1已绑定
					tell: '15089980356',
					nickName: 'KK'
				},
				{
					name: '刘武',
					state: 0, //0未绑定微信 1已绑定
					tell: '15089980356',
					nickName: 'KK'
				}
			]
		};
	},
	methods: {
		// 绑定微信
		bweixin:function(e){
			console.log(e)
			console.log("点击里绑定微信")
		},
		// 操作
		operations:function(index){
			let that = this;
			if(index === that.isShow){
				that.isShow = -1;
				return false;
			}
			that.isShow = index;
		},
		// 隐藏
		hideIsShow: function(){
			this.isShow = -1;
		},
		// 删除
		del:function(index){
			let that = this;
			let name = that.list[index].name;
			console.log(name)
			uni.showModal({
			    title: `确认删除${name}`,
			    content: '删除后无法恢复,请谨慎操作',
			    success: function (res) {
			        if (res.confirm) {
			            // console.log('用户点击确定');
						that.list.splice(index,1)
			        } else if (res.cancel) {
			            console.log('用户点击取消');
			        }
			    }
			});
		},
		// 换绑
		change:function(index){
			console.log("换绑")
		},
		// 编辑
		edit:function(index){
			console.log("删除")
		}
	}
};
</script>
### 实现 uni-app 中元素左右滑动效果 为了实现在 `uni-app` 应用程序中的元素左滑显示右滑隐藏的功能,可以利用 `<uni-swipe-action>` 组件以及自定义触摸事件来完成这一目标。 #### 使用 `<uni-swipe-action>` 组件 通过配置组件属性并绑定点击事件处理函数,能够轻松创建具有滑动手势交互功能的界面。具体来说: - 利用 `:right-options` `:left-options` 属性设置滑动选项。 - 结合 `@click` 事件监听器响应用户的触控行为[^1]。 ```html <template> <uni-swipe-action> <!-- 定义右侧滑动菜单项 --> <uni-swipe-action-item :right-options="options" @click="handleSwipe"> <view>这里是默认展示的内容</view> </uni-swipe-action-item> </uni-swipe-action> </template> <script> export default { data() { return { options: [ { text: '删除', style: { backgroundColor: '#dd524d' } }, // 更多操作按钮... ] }; }, methods: { handleSwipe(index, instance) { console.log('触发了第', index + 1, '个按钮'); // 根据实际业务逻辑调整此处代码 setTimeout(() => { instance.close(); // 关闭当前实例 }, 0); } } }; </script> ``` 上述示例展示了如何使用预构建好的 swipe action 插件实现基本的滑动效果。然而对于更复杂的场景——即希望直接控制某个特定 DOM 节点可见性的变化,则需进一步定制化解决方案。 #### 自定义触摸手势检测 当需要更加灵活地管理元素状态时,可以通过 JavaScript 手动捕捉触摸起点与终点坐标差值的方式判断用户意图,并据此改变相应视图的状态。下面是一个简单的例子说明怎样做到这一点: ```javascript data(){ return{ startX: null, isShowingElement: false } }, methods:{ start(event){ if (event.touches.length === 1) { this.startX = event.touches[0].clientX; } }, end(event, path='/'){ const endX = event.changedTouches[0].clientX; const diff = endX - this.startX; if(Math.abs(diff)>100){ if(diff > 0 && !this.isShowingElement){ // 左滑动作:显示元素 this.isShowingElement=true; } else if(diff < 0 && this.isShowingElement){ // 右滑动作:隐藏元素 this.isShowingElement=false; } } this.startX=null; // 清除起始位置以便下次计算 } } ``` 此方法允许开发者完全掌控何时何地执行何种动画或样式更改,从而更好地适应不同应用场景下的需求[^2][^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值