K
K
krll-k2015-09-15 00:31:49
JavaScript
krll-k, 2015-09-15 00:31:49

The code from the example from the article from Habr does not work, what is the error, or is it just me?

f29f40c1f0d049cc8f8e0de6725dd280.png
The code from the example from habr tyrk :

var touchstartX = 0;
var touchstartY = 0;
var touchendX = 0;
var touchendY = 0;

var gesuredZone = document.getElementById('gesuredZone');

gesuredZone.addEventListener('touchstart', function(event) {
    touchstartX = event.screenX;
    touchstartY = event.screenY;
}, false);

gesuredZone.addEventListener('touchend', function(event) {
    touchendX = event.screenX;
    touchendY = event.screenY;
    handleGesure();
}, false); 

function handleGesure() {
    var swiped = 'swiped: ';
    if (touchendX < touchstartX) {
        alert(swiped + 'left!');
    }
    if (touchendX > touchstartX) {
        alert(swiped + 'right!');
    }
    if (touchendY < touchstartY) {
        alert(swiped + 'down!');
    }
    if (touchendY > touchstartY) {
        alert(swiped + 'left!');
    }
    if (touchendY == touchstartY) {
        alert('tap!');
    }
}

My slightly tweaked version of tyrk (touchstart works, but touchend doesn't). The matter is that the example constantly deduces tap!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VinZzz, 2018-02-09
@VinZzz

Stepped on the same rake. Mouse works great, touch doesn't. Began to step-by-step display alert'om values ​​on the screen. The touchstart and touchend events themselves work. Then I tried to output event.screenX and got Undefined . It turns out that the event.screenX construct is not quite what we need for Touch . I rummaged through various articles. Found three properties: touches , targetTouches , changedTouches . I did this:
for touchstart : for touchend :
Well for Y respectively.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question