MUI事件委托拖拽

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<link rel="stylesheet" type="text/css" href="./css/mui.css"/>
</head>
<style>
	.box{
		width: 100%;
		height: 100vh;
	}
	.box1,.box2,.box3,.box4{
		width: 100px;
		height: 100px;
		position: absolute;
	}
	.box1{
		top: 0px;
		left: 0px;
		background: red;
	}
	.box2{
		top: 0px;
		left: 200px;
		background: blue;
	}
	.box3{
		top: 200px;
		left: 0px;
		background: green;
	}
	.box4{
		top: 200px;
		left: 200px;
		background: yellow;
	}
</style>
<body>
	<div class="box">
		<div class="box1"></div>
		<div class="box2"></div>
		<div class="box3"></div>
		<div class="box4"></div>
	</div>
	<script src="./js/mui.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
	mui.init({
	  	gestureConfig:{
		   tap: true, 
		   doubletap: true, 
		   longtap: true, 
		   swipe: true, 
		   drag: true, 
		   hold:false,
		   release:false
	  	}
	});
	const box = document.querySelector('.box')
	const box1 = document.querySelector('.box1')
	const box2 = document.querySelector('.box2')
	const box3 = document.querySelector('.box3')
	const box4 = document.querySelector('.box4')
	let dx = 0 , dy = 0;
	box.addEventListener('touchstart',(e)=>{
		if(e.target === box1){
			dx = e.touches[0].clientX - box1.getBoundingClientRect().left
			dy = e.touches[0].clientY - box1.getBoundingClientRect().top
		}
		if(e.target === box2){
			dx = e.touches[0].clientX - box2.getBoundingClientRect().left
			dy = e.touches[0].clientY - box2.getBoundingClientRect().top
		}
		if(e.target === box3){
			dx = e.touches[0].clientX - box3.getBoundingClientRect().left
			dy = e.touches[0].clientY - box3.getBoundingClientRect().top
		}
		if(e.target === box4){
			dx = e.touches[0].clientX - box4.getBoundingClientRect().left
			dy = e.touches[0].clientY - box4.getBoundingClientRect().top
		}
	})
	box.addEventListener('touchmove',(e)=>{
		if(e.target === box1){
			e.preventDefault(); // 阻止默认滚动行为
			e.target.style.left = (e.touches[0].clientX - dx) + 'px';
			e.target.style.top = (e.touches[0].clientY - dy) + 'px';
		}
		if(e.target === box2){
			e.preventDefault(); // 阻止默认滚动行为
			e.target.style.left = (e.touches[0].clientX - dx) + 'px';
			e.target.style.top = (e.touches[0].clientY - dy) + 'px';
		}
		if(e.target === box3){
			e.preventDefault(); // 阻止默认滚动行为
			e.target.style.left = (e.touches[0].clientX - dx) + 'px';
			e.target.style.top = (e.touches[0].clientY - dy) + 'px';
		}
		if(e.target === box4){
			e.preventDefault(); // 阻止默认滚动行为
			e.target.style.left = (e.touches[0].clientX - dx) + 'px';
			e.target.style.top = (e.touches[0].clientY - dy) + 'px';
		}
	})
	</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值