<!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>
07-13