我希望页面上有一个 div 元素,按下该元素会增加页面上的数字。 这相当简单,但我希望它也能在 iPad 和 Android 设备上运行。
我正在使用 div 上的 jQuery 绑定(bind)来使用 touchstart 事件。 使用 setInterval 我更新数字(我可以在每次数字增加时调用 setTimeout,但这无关紧要)。 我希望当手指(触摸)移出 div 时清除间隔。不幸的是,直到手指从屏幕上释放后,touchend 才会被调用。 Mobile safari 或 webkit 似乎不支持 touchleave。
我的问题是:关于如何模仿 touchleave 有什么想法吗?
最佳答案
根据上面的答案,这就是我所做的。
var $this = $('elementselector');
var fnmove = function (e) {
e.preventDefault();
e = e.originalEvent;
var touch = e.touches[0] || e.changedTouches[0];
if (!_isInBounds(touch, $this.offset(), $this.outerWidth(), $this.outerHeight())) {
$this.trigger(touch_leave_event);
$this.unbind(touch_move_event, fnmove);
};
};
$this.bind(touch_move_event, fnmove);
--入站函数
function _isInBounds(touch, elemposition, width, height) {
var left = elemposition.left,
right = left + width,
top = elemposition.top,
bottom = top + height,
touchX = touch.pageX,
touchY = touch.pageY;
return (touchX > left && touchX < right && touchY > top && touchY < bottom);
};
关于android - DOM 元素的 TouchLeave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7002851/