Skip to content

Instantly share code, notes, and snippets.

@guotingchao
Forked from kejun/gist:3358036
Last active December 15, 2015 11:09
Show Gist options
  • Save guotingchao/5251178 to your computer and use it in GitHub Desktop.
Save guotingchao/5251178 to your computer and use it in GitHub Desktop.
ios/android兼容mousemove方法
;(function($){
$.support.touch = 'ontouchend' in document;
if (!$.support.touch) {
return;
}
var eventMap = {
click: 'touchend',
mousedown: 'touchstart',
mouseup: 'touchend',
mousemove: 'touchmove'
};
var simulateEvent = function(eventType) {
$.event.special[eventType] = {
setup: function() {
var el = $(this);
el.bind(eventMap[eventType], $.event.special[eventType].handler);
if (this.nodeName === 'A' && eventType === 'click') {
this.addEventListener('click', function(e){ e.preventDefault(); }, false);
}
},
teardown: function() {
$(this).unbind(eventMap[eventType], $.event.special[eventType].handler);
},
handler: function(e) {
var touch = e.originalEvent.changedTouches[0];
e.type = eventType;
e.pageX = touch.pageX;
e.pageY = touch.pageY;
e.clientX = touch.clientX;
e.clientY = touch.clientY;
$.event.handle.call(this, e);
}
};
};
$.fn.delegate = function(selector, types, data, fn) {
var params = data;
if (typeof data === 'function') {
fn = data;
params = null;
}
var handler = function(e) {
if (this.nodeName === 'A' && e.type === 'click') {
this.addEventListener('click', function(e){ e.preventDefault(); }, false);
}
fn.apply(this, arguments);
};
return this.live(types, params, handler, selector);
};
$.each(['click', 'mousedown', 'mousemove', 'mouseup'],
function(i, name) {
simulateEvent(name);
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment