這可能是矯枉過正,但這些都是一些jQuery的擴展,我使用促進跨瀏覽器的觸摸事件:
jQuery.fn.touchstart = function (callback) {
this.each(function() {
var _self = jQuery(this);
var onTouchstart = function (ev) {
if (ev.originalEvent) {
if (ev.originalEvent.targetTouches) {
AddTouchPropertiesToJQEventObject(ev);
callback.call(this, ev);
}
}
};
_self.bind('touchstart', onTouchstart);
});
return this;
};
jQuery.fn.touchmove = function (callback) {
this.each(function() {
var _self = jQuery(this);
var onTouchMove = function (ev) {
if (ev.originalEvent) {
if (ev.originalEvent.targetTouches) {
AddTouchPropertiesToJQEventObject(ev);
callback.call(this, ev);
}
}
};
_self.bind('touchmove', onTouchMove);
});
return this;
};
jQuery.fn.touchend = function (callback) {
this.each(function() {
var _self = jQuery(this);
var onTouchEnd = function (ev) {
var lastTouchMoveInfo = _self.data('lastTouchMoveInfo');
ev = $.extend(ev, lastTouchMoveInfo);
callback.call(this, ev);
};
var onTouchMove = function (ev) {
var lastTouchMoveInfo = {
pageX: ev.pageX,
pageY: ev.pageY,
touches: ev.touches
};
_self.data('lastTouchMoveInfo', lastTouchMoveInfo);
};
_self.touchstart(onTouchMove);
_self.touchmove(onTouchMove);
_self.bind('touchend', onTouchEnd);
});
return this;
};
var AddTouchPropertiesToJQEventObject = function (jqEventObject) {
jqEventObject.pageX = jqEventObject.originalEvent.targetTouches[0].pageX;
jqEventObject.pageY = jqEventObject.originalEvent.targetTouches[0].pageY;
jqEventObject.rotation = jqEventObject.originalEvent.rotation;
jqEventObject.scale = jqEventObject.originalEvent.scale;
jqEventObject.targetTouches = jqEventObject.originalEvent.targetTouches;
jqEventObject.touches = jqEventObject.originalEvent.touches;
};
基本上你要綁定到touchstart
,touchmove
和touchend
。或者,也許只是touchstart
爲你在做什麼。
http://jquerymobile.com/ – 2012-03-05 17:06:43