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