Answer the question
In order to leave comments, you need to log in
How to implement swipe in native javascript?
I'm struggling with the fact that I don't know how to calculate an absolutely relative shift (newX, newY):
document.body.addEventLestener("touchmove",function(e){
if(event.targetTouches.length==1){
var touch = event.targetTouches[0];
var x = touch.pageX, y = touch.pageY;
var newX = ???, newY = ???;
if(x-newX <= y-newY) ...
}
},false);
Answer the question
In order to leave comments, you need to log in
We need to add not only touchmove, but also touchstart and touchend.
If you look only along the x-axis, then
var touchStart= -1,
touchEnd= -1,
count= 0,
targetElem= document.getElementById('some_element');
function tStart(e){
e = e ? e : window.event;
e = ('changedTouches' in e)?e.changedTouches[0] : e;
touchStart = e.pageX;
}
function tMove(e){
e = e ? e : window.event;
e = ('changedTouches' in e)?e.changedTouches[0] : e;
touchEnd = e.pageX - touchStart;
var _count = count -touchEnd;
targetElem.style.transform = 'translateX('+(-_count)+'px)';
}
function tEnd(e){
count = count - touchEnd;
targetElem.style.transform = 'translateX('+(-count)+'px)';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question