touchstart
and touchmove
with Microsoft's non-standard vision for this. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
#swipe { -ms-touch-action: none; }
X-UA-Compatible
controls the mode of displaying pages in IE8 + browsers. var IE = window.navigator.msPointerEnabled;
var touchElem = document.getElementById('swipe'); if (IE && window.MSGesture) { //events for IE var eventStart = "MSGestureStart", eventMove = "MSGestureChange"; var msGesture = new MSGesture(); msGesture.target = touchElem; touchElem.addEventListener('MSPointerDown', function(event) { msGesture.addPointer(event.pointerId); }); } else { //events for other browsers var eventStart = "touchstart", eventMove = "touchmove"; } touchElem.addEventListener(eventStart, function (e) { swipe.init(e, eventMove); }, false);
swipe.init
method for non-IE, we remember the starting coordinates of the touch event, and then all browsers move to the swipe.onTouchMove
method if(!IE) { if (e.pageX || e.targetTouches[0].pageX) { swipe.variable.startX = e.pageX || e.targetTouches[0].pageX; } } touchElem.addEventListener(eventMove, swipe.onTouchMove, false);
swipe.onTouchMove
there is also a separation by browser. if(IE) { if (e.detail == e.MSGESTURE_FLAG_INERTIA) return; swipe.variable.translationX += e.translationX; } else { var newX = e.pageX || e.targetTouches[0].pageX; swipe.variable.translationX = newX - swipe.variable.startX; } if (Math.abs(swipe.variable.translationX) > 10) { swipe.switchDirection(); }
10
is the offset in px, which occurred at the time of the gesture on the screen. It can be set arbitrary. The sensitivity of the touch response will depend on its magnitude.switchDirection
function, the direction of the gesture is determined, if the value of swipe.variable.translationX
positive, then the swiperight
event was swiperight
, and if negative, then swipeleft
. $('#swipe').gestureSwipe('event', eventHandler);
event
can be swipeleft
or swiperight
Source: https://habr.com/ru/post/222247/
All Articles