<! DOCTYPE html>
<meta charset= "Utf-8" >
<!--settings viewport-->
<meta name= "viewport" content= "width=device-width,initial-scale=1.0,user-scalable=0"/>
<style>
. div{
width:100px;
height:50px;
position:fixed;
background-color:red;
border-radius:25px;
}
</style>
<body>
<div class= "div" id= "div" ></div>
</body>
<script>
Window.onload=function () {
var flag = 0; Mark is dragged or clicked
var odiv = document.getElementById (' div ');
var disx,movex,l,t,starx,stary,starxend,staryend;
Odiv.addeventlistener (' Touchstart ', function (e) {
Flag = 0;
E.preventdefault ()//Prevent scrolling of pages when touching, zooming
DISX = E.touches[0].clientx-this.offsetleft;
Disy = E.touches[0].clienty-this.offsettop;
The coordinates when the finger is pressed
Starx = E.touches[0].clientx;
StarY = E.touches[0].clienty;
Console.log (DISX);
});
Odiv.addeventlistener (' Touchmove ', function (e) {
flag = 1;
L = E.TOUCHES[0].CLIENTX-DISX;
T = E.touches[0].clienty-disy;
The difference between the current position and the start position when moving
Starxend = E.touches[0].clientx-starx;
Staryend = e.touches[0].clienty-stary;
Console.log (L);
if (l<0) {//limit dragged X range, cannot drag out the screen
L = 0;
}else if (L > Document.documentelement.clientwidth-this.offsetwidth) {
L=document.documentelement.clientwidth-this.offsetwidth;
}
if (t<0) {//limit dragged Y range, cannot drag out the screen
t=0;
}else if (t>document.documentelement.clientheight-this.offsetheight) {
T = Document.documentelement.clientheight-this.offsetheight;
}
MoveX = L + ' px ';
Movey = T + ' px ';
Console.log (MoveX);
This.style.left = MoveX;
This.style.top = Movey;
});
Window.addeventlistener (' Touchend ', function (e) {
Alert (parseint (MoveX))
Judging the sliding direction
if (flag = = = 0) {//Tap
window.location.href= ' http://www.baidu.com ';
}
});
}
</script>
Mobile can drag hover window + Click event