uni 实现向左滑动出现删除

效果图:

 

 

templete:

<template>
			<view class="content-1">
				<view class="container-1" @touchstart="touchS" @touchmove="touchM" @touchend="touchE"
					 :style="{'left':act_touch==index?leftStyle + 'upx':0}" :data-index="index" v-for="(i, index) in 3">
					<view>
						编译成功编译成功编译成功编译成功编译成功
					</view>
					<view class="delete-1" @click="delData">
						删除
					</view>
				</view>
			</view>
		</template>

 data:

leftStyle: 0,
				startX: 0,
				hiddenFlag: true,
				delBtnWidth: 220,
				act_touch:null,

js:

// 开始移动时
			touchS({
				touches
			}) {
				// startX记录开始移动的位置
				if (touches.length === 1) {
					this.startX = touches[0].clientX;
				}
				// hiddenFlag为true则是从右向左,为false则是从左向右
				if (this.leftStyle === 0) {
					this.hiddenFlag = true;
				} else {
					this.hiddenFlag = false;
				}
			},
			touchM(e) {
				if (e.touches.length === 1) {
					//手指移动时水平方向位置
					const moveX = e.touches[0].clientX;
					this.moveFunc(moveX);
					//获取手指触摸的是哪一项
					console.log(e.currentTarget.dataset.index)
					var index = e.currentTarget.dataset.index;
					this.act_touch = index;
				}
			},
			touchE({
				touches
			}) {
				const {
					leftStyle
				} = this;
				const {
					delBtnWidth
				} = this;
				// 如果停止滑动的距离大于二分之一则直接弹出删除按钮,不然则left为0
				if (-leftStyle > delBtnWidth / 2) {
					this.leftStyle = -delBtnWidth;
				} else {
					this.leftStyle = 0;
				}
			},
			moveFunc(moveX) {
				// 原始位置向左,leftStyle为小于0;原始位置向右,leftStyle为大于0
				// disX为相对最初位置的左右距离
				// 大于0为向右,小于0为向左
				const disX = moveX - this.startX;
				const delBtnWidth = this.delBtnWidth;
				let offsetNum = 0;
				if (-disX >= delBtnWidth && this.leftStyle === -delBtnWidth) {
					return;
				}
				console.log(disX, this.hiddenFlag);
				// this.hiddenFlag为true则是从左到右,则应该将container向左移动
				// this.hiddenFlag为false则是从右向左,则应该将container向右移动
				if (this.hiddenFlag) {
					// 此时container为最右边,则应该将container向左移动
					// disX大于0为相对原始位置的向右移动,则直接将offsetNum置为0
					// 否则为向左移动,offsetNum为disX相反的值,判断是否超过设置的最大位置
					if (disX == 0 || disX > 0) {
						offsetNum = 0;
					} else {
						offsetNum = disX;
						if (disX <= -delBtnWidth) {
							//控制手指移动距离最大值为删除按钮的宽度
							offsetNum = -delBtnWidth;
						}
					}
				} else {
					// 此时container为最左边,应该向右移动
					// disX小于0为相对原始位置的向左移动,则直接将offsetNum置为-this.delBtnWidth
					// 否则为相对原始位置的向右移动,此时应该将最大位置与向右位置的差值为此刻位置,判断是否为大于0
					if (disX < 0) {
						offsetNum = -this.delBtnWidth;
					} else {
						offsetNum = -this.delBtnWidth + disX;
						if (offsetNum > 0) {
							offsetNum = 0;
						}
					}
				}
				this.leftStyle = offsetNum;
			},
			//删除方法
			delData() {
				console.log("删除")
				uni.showModal({
					title: '提示',
					content: "确认删除该职位?",
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

css:

.content-1{
		width: 100%;
		overflow-x: hidden;
		background-color: #f4f4f4;
		border-radius: none;
	}
	.container-1 {
		position: relative;
		margin-top: 26rpx;
		margin-bottom: 20upx;
		background-color: #fff;
		padding:20upx;
	}
	.delete-1{
		position: absolute;
		top: 50%;
		-webkit-transform: translateY(-50%);
		transform: translateY(-50%);
		right: -107px;
		width: 65px;
		height: 38px;
		line-height: 39px;
		font-weight: 500;
		font-size: 16px;
		text-align: center;
		color: #FFFFFF;
		background: #FF1C1C;
		width: 106px;
	}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
基于微信小程序的健康管理(小程序+ssm完整源码+演示视频) 基于微信小程序的健康管理 功能需求: - [ ] 满足心理健康检测需要题库随机抽题,首次注册完成做题给予初步评价,评价结果反应给管理员,不同班级分配给主管本班级的管理员。 - [ ] 提供一个留言板块可匿名和实名,可以选择只给管理员看和给好友看,可回复留言 - [ ] 管理员与学生可以直接发消息交流 - [ ] 出现心理健康问题人员要及时向管理员发出提示 - [ ] 学生首次注册要完善个人信息统一为多级,管理者1编号2编号 一个学生设置两名管理者,班级编号 - [ ] 该小程序主要包括 3 个主体界面模块;“首页模块”、“留言与聊天模块”、“我的模块”。其中首页模块主要包括三大导航栏:“心理健康测查”、“心理健康小故事”、“我的心理健康状态”。其中“心理健康测查”主要用于测查学生心理健康状况分为随机测查和固定时间测查保证能及时与准确了解学生心理健康情况;“心理健康小故事”栏目展示关于心理健康的故事分享用于提高学生心理健康水平;“我的心理健康状态”栏目展示学生目前心理健康情况和问题。 - [ ] 管理员主要为管理学生心理健康不需要进行心理健康教育,因此登陆时需要有学生与管理者的区分 - [ ] 最对一个班级所有学生心理健康状态进行统计,推送给管理员以图形形式。 - [ ] 在做程序的时候最好带上注释,可以让我看的明白 ,什么模块时什么东西。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值