Article From:https://segmentfault.com/q/1010000011701267
Question:

Add the touch move method to your dom. Increase the forward sliding temperature, decrease the backward sliding temperature. Now increase the sliding temperature. How do you tell him to slide backwards and then perform the temperature reduction method?

`` //Temperature skid adjustment\$("#btn").On ("touchstart", function (E) {Var BTN = \$("#btn");E.preventDefault ();If (e.targetTouches.length = = 1) {Var touch = e.targetTouches[0];Var disX=touch.pageX-btn.offset ().Left;Var disY=touch.pageY-btn.offset ().Top;Var bar= \$\$("#bar");Var width= bar.clientWidth-btn.offset ().Width;Btn.on ("touchmove", move);Function move (E) {Var touch2=event.targetTouches[0];Var l=touch2.pageX-disX;Var t=tOuch2.pageY-disY;If (l< 0) {L=0;};If (l> width) {L=width;};If (t< 0) {T=0;};If (t> 0) {T=0;};Btn.css ("left", l+'px');BTN.css ("top", t+'px');};Btn.on ("touchend", Chend);Function Chend (E) {Btn.off ("touchmove", move);Btn.off ("touchend", Chend);};};}, false);``

Save x.y of x.y and end of start to calculate.

If you simply determine the direction, you can listen for touchstart events and touchend events. Get the start coordinates startX, start Y, and end coordinates endX, endY. Then endX-start X can determine left and right, endY-starTY can judge up and down. Of course, this can not determine the direction of the sliding process. If you want to make a detailed judgment of the direction, you can refer to the source implementation of iscoll.