zepto的touch模块解决click延迟300ms问题以及点透问题的详解
大家都知道移动端的click事件会延迟300ms触发,这时大家可以使用zepto的touch模块,里面定义了一个tap事件,通过绑定tap事件,可以实现点击立即触发的功能。
那么,它的tap事件是怎么实现的呢?这是我们要解决的第一个问题。
第二个问题,大家都知道zepto的tap事件会有点透的问题,那么,点透如何出现,点透为什么会出现,点透问题如何解决等,这是我们要解决的第二个问题。
我们先来看tap事件是如何实现的?
查看touch.js代码,在最后的代码中有以下代码:
;['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',
'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName){
$.fn[eventName] = function(callback){ return this.on(eventName, callback) }
})
上面的代码,是把tap函数,赋给zepto的原型对象。因此,使用tap事件处理函数,有以下两种方法:
1.$("div").tap(function(){})
2.$("div").on("tap",function(){})
接下来,我们来查看,touch.js是如何实现tap自定义事件的。
$(document)
.bind('MSGestureEnd', function(e){
var swipeDirectionFromVelocity =
e.velocityX > 1 ? 'Right' : e.velocityX < -1 ? 'Left' : e.velocityY > 1 ? 'Down' : e.velocityY < -1 ? 'Up' : null;
if (swipeDirectionFromVelocity) {
touch.el.trigger('swipe')
touch.el.trigger('swipe'+ swipeDirectionFromVelocity)
}
})
.on('touchstart MSPointerDown pointerdown', function(e){
if((_isPointerType = isPointerEventType(e, 'down')) &&
!isPrimaryTouch(e)) return
firstTouch = _isPointerType ? e : e.touches[0]
if (e.touches && e.touches.length === 1 && touch.x2) {
// Clear out touch movement data if we have it sticking around
// This can occur if touchcancel doesn't fire due to preventDefault, etc.
touch.x2 = undefined
touch.y2 = undefined
}
now = Date.now()
delta = now - (touch.last || now)
touch.el = $('tagName' in firstTouch.target ?
firstTouch.target : firstTouch.target.parentNode)
touchTimeout && clearTimeout(touchTimeout)
touch.x1 = firstTouch.pageX
touch.y1 = firstTouch.pageY
if (delta > 0 && delta <= 250) touch.isDoubleTap = true
touch.last = now
longTapTimeout = setTimeout(longTap, longTapDelay)
// adds the current touch contact for IE gesture recognition
if (gesture && _isPointerType) gesture.addPointer(e.pointerId);
})
.on('touchmove MSPointerMove pointermove', function(e){
if((_isPointerType = isPointerEventType(e, 'move')) &&
!isPrimaryTouch(e)) return
firstTouch = _isPointerType ? e : e.touches[0]
cancelLongTap()
touch.x2 = firstTouch.pageX
touch.y2 = firstTouch.pageY deltaX += Math.abs(touch.x1 - touch.x2)
deltaY += Math.abs(touch.y1 - touch.y2)
})
.on('touchend MSPointerUp pointerup', function(e){
if((_isPointerType = isPointerEventType(e, 'up')) &&
!isPrimaryTouch(e)) return
cancelLongTap() // swipe
if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
(touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)) swipeTimeout = setTimeout(function() {
touch.el.trigger('swipe')
touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
touch = {}
}, 0) // normal tap
else if ('last' in touch)
// don't fire tap when delta position changed by more than 30 pixels,
// for instance when moving to a point and back to origin
if (deltaX < 30 && deltaY < 30) {
// delay by one tick so we can cancel the 'tap' event if 'scroll' fires
// ('tap' fires before 'scroll')
tapTimeout = setTimeout(function() { // trigger universal 'tap' with the option to cancelTouch()
// (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
var event = $.Event('tap')
event.cancelTouch = cancelAll
touch.el.trigger(event) // trigger double tap immediately
if (touch.isDoubleTap) {
if (touch.el) touch.el.trigger('doubleTap')
touch = {}
} // trigger single tap after 250ms of inactivity
else {
touchTimeout = setTimeout(function(){
touchTimeout = null
if (touch.el) touch.el.trigger('singleTap')
touch = {}
}, 250)
}
}, 0)
} else {
touch = {}
}
deltaX = deltaY = 0 })
上面的代码,大家都知道,是在document上绑定了很多事件,其中,我们只要关注touchstart和touchend 这两个事件绑定。
在touchend的事件处理函数中,有以下代码:
tapTimeout = setTimeout(function() {
// trigger universal 'tap' with the option to cancelTouch()
// (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
var event = $.Event('tap')
event.cancelTouch = cancelAll
touch.el.trigger(event)
// trigger double tap immediately
if (touch.isDoubleTap) {
if (touch.el) touch.el.trigger('doubleTap')
touch = {}
}
// trigger single tap after 250ms of inactivity
else {
touchTimeout = setTimeout(function(){
touchTimeout = null
if (touch.el) touch.el.trigger('singleTap')
touch = {}
}, 250)
}
}, 0)
以上代码就会立即触发一个自定义的tap事件。这时绑定的tap事件回调函数就会立即执行。
因为点击事件,就是touchstart和touchend的组合。
当touchend触发时,就代表一次点击结束,因此在touchend的回调函数中,触发一个自定义的tap事件,相当于触发了tap事件,因此马上就会执行,而不会出现click事件延迟300ms。
大家都知道,使用zepto的tap事件,会出现点透的问题。
我们先来了解下,什么叫点透问题?
假如你在列表页面上创建一个弹出层,弹出层有个关闭的按钮(绑定了tap事件),你点了这个按钮关闭弹出层后,这个按钮正下方的内容也会执行点击事件(或打开链接)。这个就是一个“点透”现象。
点透出现的原因是:延迟300ms的click事件触发了。
zepto的touch模块中,没有对这个延迟300ms的click事件取消,或者取消不了。
而fastClick中,会对这个延迟300ms的click事件取消,也就是这个click事件不会触发。
所以zepto的tap事件(通过touchstart和touchend模拟出来的)有点透问题,而fastClick的click事件(通过touchstart和touchend模拟出来的)没有。
zepto中,没有对真实的延迟300ms的click事件做处理,或者做了处理,但是还是触发了。而fastClick对真实的延迟300ms的click事件做了处理,不会触发。
深入到zepto的touch.js和fastClick的源码,我们可以得知:
zepto的tap事件和fastClick的click事件,源码差不多。
为什么基本相同的代码,zepto会点透而fastclick不会呢?
原因是zepto的代码里面有个settimeout,在settimeout里面执行e.preventDefault()不会生效,因此zepto中的延迟300ms的click事件会触发,而fastClick不会。
解决tap点透问题:
1.使用fastclick,不过你之前写的tap事件,都要改成click事件。
2.使用基于zepto的tap插件,此插件,代码量不大,也不用修改你写的tap事件,需要的请问我要,谢谢。
加油!
zepto的touch模块解决click延迟300ms问题以及点透问题的详解的更多相关文章
- zepto引用touch模块后,click失效
近日,有个拼图小活动,引用了zepto,以及zepto的touch模块. 在拼图结束之后,进行抽奖的活动,该抽奖结果是以弹框展示. 这里的关闭按钮需要添加点击事件: $(document.body). ...
- Zepto.js touch模块深入分析
目的:记录 Zepto.js touch模块 源码阅读 源码: // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely ...
- Zepto.js touch模块深入分析 解决手机点击事件
源码: // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT li ...
- nginx应用场景,特性,目录结构,常用模块,内置变量,URL和URI,http状态码,配置文件详解
1.nginx介绍 1丶俄罗斯人开发的,开源www服务软件 2丶软件一共780K 3丶nginx本身是一款静态(html,js,css,jpg等)www软件 4丶静态小文件高并发,同时占用的资源很少, ...
- 系统丢失的DLL文件问题根源解决(纯净官网下载放心)(图文详解)(博主推荐)
导言 最近,身边的朋友们,问我,他电脑的win10系统里 mfc110.dll 丢失. 其他的系统文件丢失修复,是一样的步骤. 现象 大家也许,都会有这么一个习惯,动不动则就去百度上搜索. 其实啊,这 ...
- 移动端的silder,未封装,基于zepto的touch模块,有参照修改过touch的bug
<!--html模块--> <header class="appoin-head"> <ul> <li class="aa&qu ...
- 【精选】Nginx模块Lua-Nginx-Module学习笔记(一)Nginx Lua API 接口详解
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一.介绍 各种* _by_lua,* _by_lua_block和* _by_lua_file配置指令用 ...
- Nginx模块Lua-Nginx-Module学习笔记(一)Nginx Lua API 接口详解
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一.介绍 各种* _by_lua,* _by_lua_block和* _by_lua_file配置指令用 ...
- Python3中正则模块re.compile、re.match及re.search函数用法详解
Python3中正则模块re.compile.re.match及re.search函数用法 re模块 re.compile.re.match. re.search 正则匹配的时候,第一个字符是 r,表 ...
随机推荐
- float和double在内存中的存储方式
本文转载于:http://wenku.baidu.com/link?url=ARfMiXVHCwCZJcqfA1gfeVkMOj9RkLlR9fIexbgs9gDdV8rIS48A1_xe1y6YgX ...
- 深入理解Session与Cookie
Session与cookie的作用都是为了保持访问用户与后端服务器的交互状态. cookie通过把所有要保存的数据通过HTTP协议的头部从客户端传递到服务端,又从服务端再传回到客户端,所有的数据都存储 ...
- The project: project which is referenced by the classpath, does not exist.
有个java工程运行时报The project: project which is referenced by the classpath, does not exist.错误 上网查解决方案,说是将 ...
- 我用VS2012在Nuget中安装Signalr之后报错
我用VS2012在Nuget中安装Signalr之后报错 “/”应用程序中的服务器错误. The following errors occurred while attempting to load ...
- 【摘】linux中fstab解说
https://wiki.archlinux.org/index.php/Fstab_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 一个简单的 /etc/fstab,使 ...
- 关于handler
1. 一个Handler只有一个队列;2. 在调用Handler.post(Runnable runnable)方法时,会将runnable封装成一个Message; 3. 在队列执行时,会判断当前的 ...
- PHP5.6.15连接Sql Server 2008配置方案
php5.6的如果想连接Sql Server 2008数据库,需要手动配置扩展和安装一个驱动. 下载SQL Server Driver for PHP的扩展包,64位系统的官方不支持,找到一个非官方的 ...
- Linux里的2>&1
我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...
- ios调用本地拨打电话,发送短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicat ...
- Type.GetType(string)为空
Type type = Type.GetType(scheduleJob.JobType); 时type为空, 导致执行下一步时 MethodInfo method = type.GetMethod( ...