user4532193
user4532193

Reputation:

Javascript - touchstart and touchend locations

I would like to get the touchstart and touchend locations if element "#menu" is touched.

I wrote this code, but the event doesn't seem to trigger at all:

        var lastLoc = 0;

        $(document).on('touchstart', '#menu', function(e) {
            lastLoc = e.originalEvent.touches[0].pageX;
        });

        $(document).on('touchend', '#menu', function(e) {
            var newLoc = e.originalEvent.touches[0].pageX;
            alert("lastLoc = "+lastLoc+", newLoc = "+newLoc);
        });

As far as I know this should work.

Thanks in advance.

Upvotes: 1

Views: 1722

Answers (3)

user4532193
user4532193

Reputation:

Sorry for your trouble, I ended up using jquery mobile's "swipeleft" Thanks anyway :)

Upvotes: 0

Oka
Oka

Reputation: 26355

The touchend event should, logically, have zero touch points, only changedTouches.

Here's a demo.

function startHandler (e) {
    window.console.log('Start:', e.originalEvent.touches[0].pageX);
}

function endHandler (e) {
    window.console.log('End:', e.originalEvent.changedTouches[0].pageX);
}



$(document).on('touchstart', '#menu', startHandler);
$(document).on('touchend', '#menu', endHandler);

See also: Simulating touchstart and touchend events? if you are having trouble testing touch events.

Upvotes: 2

julio
julio

Reputation: 2937

You need to test the touch event on a touchable surface such as a smartphone or a tablet. In Google Chrome there is a way to simulate this function.

Upvotes: 0

Related Questions