Mobile can drag hover window + Click event

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.