JS to judge the left and right of the phone slide

Source: Internet
Author: User

JS Code

$ (function () {function judge () {varStartX//let StartX be a global variable in the Touch event function.         varEndX; varEl = document.getElementById ('io');//Touch area. function cons () {//Isolated encapsulation This event guarantees the order of execution, allowing access to the data that should be accessed.             if(StartX > EndX) {//judging left and right moving programsAlert" Left"); } Else{alert (" Right"); }} el.addeventlistener ('Touchstart', Function (e) {varTouch =e.changedtouches; StartX= touch[0].clientx; Starty= touch[0].clienty;          }); El.addeventlistener ('Touchend', Function (e) {varTouch =e.changedtouches; EndX= touch[0].clientx; Endy= touch[0].clienty;  Cons (); //startx EndX data collection should be performed after the Touchend event, rather than in the Touchstart event, to access Touchstart and touchend data generated by the 2 events startx and EndX.  In addition, STARTX and EndX are global in the _touch event function, so they can be accessed in this function, so it is only necessary to pay attention to the sequence of event execution.         });  } judge (); })  

HTML casually write can remember ID to correspond

<body>      <div style= "width:100%;height:3000px;background: #08c;" id= "io" >        </div>  < /body>  

  

JS to judge the left and right of the phone slide

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.