您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

为jQuery添加Webkit的触摸的方法分享

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 这段代码是我在做13年一份兼职的时候无聊加上去的,为jQuery添加触摸事件的支持。因为做得有点无聊,所以就帮客户添加了用响应式网页+JS touch兼容了移动设备,主要是Webkit的移动设备

这段代码是我在做13年一份兼职的时候无聊加上去的,为jQuery添加触摸事件的支持。因为做得有点无聊,所以就帮客户添加了用响应式网页+JS touch兼容了移动设备,主要是Webkit的移动设备。   这里就分享下我的实现。 先贴上代码:    代码如下: //Published by Indream Luo //Contact: [email protected] //Version: Chinese 1.0.0   !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);       网上能找到很多关于Touch事件的相关信息,所以我就不详细说明了,可以解释得简单一点。   触摸事件代替鼠标事件 在Webkit移动设备上,触摸操控首先会触发触摸事件,在0.5秒后才会触摸鼠标事件。   个人觉得这在设计上可以理解。先满足于触摸操控的需求,然后再向“下”兼容鼠标事件,以满足原有面向桌面网页的使用。   所有的事件大致执行顺序是:touchstart->touchmove->touchend->0.5s->鼠标事件mouseover/scroll/click等   按照webkit移动浏览器的设计,一般开发时候按照桌面网页开发,然后移动设备上使用是没问题的。不过桌面上大量使用的hover类效果时常会因为触摸把mouse事件+click事件触发个遍而悲剧;0.5秒的延迟也对用户体验造成了大伤害。   所以我添加了tapOrClick事件,用途就是替代click事件,并且灭了那0.5秒。   滚动锁定 在用户使用触摸设备进行滚动,而触摸已经停止的时候,浏览器会锁定整个页面,暂停所有UI资源占用,而把大部分资源留给内核进行滚动。同样的情况还会发生在放大缩小页面内容,甚至更甚。   因为要加个滚动渐变的特效,所以我添加了moveOrScroll事件,在滑动的时候执行滚动中应该执行的