uniapp超级丝滑的滑块效果,可跟随手指拖动

<template>
	<div class="container" @touchstart="touchStart" @touchend="touchEnd" @touchmove="handletouchmove">
		<div class="content">
			内容部分
		</div>
		<div class="slider" :style="{ transform: `translateX(${sliderPosition}px)` }">
			侧滑部分
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				isSliding: false, //是否在进行侧滑操作
				startX: 0, //启示位置
				sliderPosition: -200, //侧滑元素(滑块)的当前横向位置
				// currentX: 0,
				startXs: 0,
				endX: 0,
			};
		},
		methods: {
			touchStart(event) {
				console.log('touchStart:触摸开始', event.touches[0].clientX);
				this.isSliding = true;
				this.startX = event.touches[0].clientX;
				this.startXs = event.touches[0].clientX
			},
			touchEnd(event) {
				console.log('touchEnd,触摸结束', event.mp.changedTouches[0].clientX);
				this.isSliding = false;
				this.endX = event.mp.changedTouches[0].clientX
				// 触发滑快是否全部绽放 或者全部收起
				this.assaaa()
			},
			// 滑块向左移动了30% 用户触摸滑动
			assaaa() {
				// 终点-起点判断移动了多少
				const deltaX = this.endX - this.startXs;
				console.log('移动了,', deltaX);
				if(deltaX > -100){
					console.log('开启滑块');
					this.sliderPosition =  0;
				}
				if(deltaX < 100){
					console.log('关闭滑块');
					this.sliderPosition =  -200;
				}
			},
			handletouchmove(event) {
				console.log('handletouchmove触摸移动');
				if (this.isSliding) {
					const currentX = event.touches[0].clientX;//移动的当前位置
					const deltaX = currentX - this.startX; //移动的当前位置-开始位置 = 滑块的所在位置
					this.sliderPosition += deltaX;

					// 限制侧滑的最大宽度,可以根据需要调整
					if (this.sliderPosition > 0) {
						console.log('进来了0');
						this.sliderPosition = 0;
					}

					// 限制侧滑的最小宽度,可以根据需要调整
					if (this.sliderPosition < -200) {
						console.log('进来了-100');
						this.sliderPosition = -200;
					}
					this.startX = currentX;
					console.log('this.startX:', this.startX);
				}
			},
		},
	};
</script>

<style scoped>
	.container {
		display: flex;
		overflow: hidden;
		position: relative;
	}

	.content {
		flex: 1;
		background-color: #f0f0f0;
		height: 1000rpx;
		/* 内容部分样式 */
	}

	.slider {
		background-color: #3498db;
		width: 200px;
		/* 侧滑宽度,可以根据需要调整 */
		position: absolute;
		top: 0;
		bottom: 0;
		transform: translateX(0);
		transition: transform 0.3s;
		z-index: 1;
		/* 侧滑部分样式 */
	}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个使用JavaScript和CSS实现左右动的表格示例: ```html <!DOCTYPE html> <html> <head> <title>可左右拖动的表格</title> <style> .table-wrapper { overflow-x: auto; white-space: nowrap; } table { border-collapse: collapse; width: 100%; } th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #f2f2f2; } </style> </head> <body> <div class="table-wrapper"> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>城市</th> <th>学历</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>25</td> <td>男</td> <td>北京</td> <td>本科</td> </tr> <tr> <td>李四</td> <td>30</td> <td>女</td> <td>上海</td> <td>硕士</td> </tr> <tr> <td>王五</td> <td>28</td> <td>男</td> <td>广州</td> <td>本科</td> </tr> <tr> <td>赵六</td> <td>35</td> <td>女</td> <td>深圳</td> <td>博士</td> </tr> </tbody> </table> </div> <script> // 获取表格包裹器和表格 var tableWrapper = document.querySelector('.table-wrapper'); var table = document.querySelector('table'); // 当鼠标在表格上按下时记录位置 var isDown = false; var startX; var scrollLeft; tableWrapper.addEventListener('mousedown', function(e) { isDown = true; startX = e.pageX - tableWrapper.offsetLeft; scrollLeft = tableWrapper.scrollLeft; }); // 当鼠标移动时拖动表格 tableWrapper.addEventListener('mousemove', function(e) { if (!isDown) return; e.preventDefault(); var x = e.pageX - tableWrapper.offsetLeft; var walk = (x - startX) * 3; tableWrapper.scrollLeft = scrollLeft - walk; }); // 当鼠标松开时停止拖动 tableWrapper.addEventListener('mouseup', function() { isDown = false; }); // 当鼠标离开表格时停止拖动 tableWrapper.addEventListener('mouseleave', function() { isDown = false; }); </script> </body> </html> ``` 在这个示例中,我们首先创建了一个table-wrapper类的div元素,用于包裹表格。然后在CSS中设置了表格和单元格的样式,并将table-wrapper元素的overflow-x属性设置为auto,以便在表格宽度超出容器宽度时显示水平滚动条。 接下来,我们使用JavaScript监听table-wrapper元素上的mousedown、mousemove和mouseup事件。当按下鼠标时,我们记录当前鼠标位置和表格包裹器的scrollLeft值。然后在鼠标移动时,我们计算出鼠标相对于起始位置的偏移量,将其乘以一个常数(3)并将结果减去初始的scrollLeft值,以获得新的scrollLeft值。最后,在鼠标松开或离开表格时,我们停止拖动。 这样就可以实现一个可以左右丝拖动的表格。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源码地址

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值