Prelim caveat: I am very new to js and have mainly gotten by through doing web searches for examples/tutorials.
I am writing js which should run both on web and on mobile devices (e.g. iPad).
We have a library to help abstract away the differences between mouse
and touch
:
if (navigator.userAgent.search('Mobile') > 0) {
window.FLAGS = {
start: 'touchstart',
end: 'touchend',
move: 'touchmove',
click: 'click',
touchScreen: true
};
} else {
window.FLAGS = {
start: 'mousedown',
end: 'mouseup',
move: 'mousemove',
click: 'click',
touchScreen: false
};
}
Then in code you can do things like:
widget.view.bind(FLAGS.start, function(e) {
I am trying to find a touch
equivalent for mouseleave
so I can do a similar trick.
I can imagine ways to catch a leave
event by tracking the position on move
and comparing that to bounding box of widget in question, but I'm hoping there's a little one-liner like the touchstart
/mousedown
relationship.
It's been suggested, but not implemented AFAIK: http://www.quirksmode.org/mobile/advisoryTouch.html
Something like this might work (writing it from top of my head, untested):
var element;
document.addEventListener('touchstart', function(event) {
event.preventDefault();
var touch = event.touches[0];
element = document.elementFromPoint(touch.pageX,touch.pageY);
}, false);
document.addEventListener('touchmove', function(event) {
event.preventDefault();
var touch = event.touches[0];
if (element !== document.elementFromPoint(touch.pageX,touch.pageY)) {
touchleave();
}
}, false);
function touchleave() {
console.log ("You're not touching the element anymore");
}