这段代码是我在做13年一份兼职的时候无聊加上去的,为jQuery添加触摸事件的支持。因为做得有点无聊,所以就帮客户添加了用响应式网页+JS touch兼容了移动设备,主要是Webkit的移动设备。
这里就分享下我的实现。
先贴上代码:
!function ($) {
window.indream = window.indream || {};
$.indream = indream;
//Define events
indream.touch = {
evenList: {
touchStart: {
htmlEvent: \'touchstart\'
},
touchMove: {
htmlEvent: \'touchmove\'
},
touchEnd: {
htmlEvent: \'touchend\'
},
tapOrClick: {
eventFunction: function (action) {
$(this).each(function () {
(function (hasTouched) {
$(this).touchEnd(function (e) {
hasTouched = true;
action.call(this, e);
});
$(this).click(function (e) {
if (!hasTouched) {
action.call(this, e);
}
});
}).call(this, false);
});
return this;
}
},
moveOrScroll: {
eventFunction: function (action) {
$(this).each(function () {
(function (hasTouched) {
$(this).touchMove(function (e) {
hasTouched = true;
action.call(this, e);
});
$(this).scroll(function (e) {
if (!hasTouched) {
action.call(this, e);
}
});
}).call(this, false);
});
return this;
}
}
}
}
//Add events into jquery
for (var eventName in indream.touch.evenList) {
var event = indream.touch.evenList[eventName];
$.fn[eventName] = event.eventFunction || (function (eventName, htmlEvent) {
return function (action) {
$(this).each(function () {
$(this).bind(htmlEvent, action);
//Add event listener method for IE or others
if (this.attachEvent) {
this.attachEvent(\'on\' + htmlEvent, function (e) {
$(this).on(eventName);
});
} else {
this.addEventListener(htmlEvent, function (e) {
$(this).on(eventName);
});
}
});
return this;
}
})(eventName, event.htmlEvent);
}
}(window.jQuery);
本文地址:https://www.stayed.cn/item/11853
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我