您好,欢迎访问一九零五行业门户网

如何使用Zepto tap事件的穿透与点透(附代码)

这次给大家带来如何使用zepto tap事件的穿透与点透(附代码),使用zepto tap事件的穿透与点透注意事项有哪些,下面就是实战案例,一起来看一下。
首先,什么是zepto tap事件穿透?
tap事件穿透就是,有多个层级上有绑定事件,最上层的绑定了tap事件,下层绑定了click事件,在执行完上层事件后会触发下层事件,进而出现事件穿透。如果下层是input标签,必穿透。
究其原因:
是因为zepto实现tap事件是冒泡到document上时才触发的,也就是tap事件是绑定在document上,而click事件有延时执行。
下面我们贴下zepto.1.1.6 tap事件的源码:
;(function($){
var touch = {},
touchtimeout, taptimeout, swipetimeout, longtaptimeout,
longtapdelay = 750,
gesture
function swipedirection(x1, x2, y1, y2) {
return math.abs(x1 - x2) >=
math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'left' : 'right') : (y1 - y2 > 0 ? 'up' : 'down')
}
function longtap() {
longtaptimeout = null
if (touch.last) {
touch.el.trigger('longtap')
touch = {}
}
}
function cancellongtap() {
if (longtaptimeout) cleartimeout(longtaptimeout)
longtaptimeout = null
}
function cancelall() {
if (touchtimeout) cleartimeout(touchtimeout)
if (taptimeout) cleartimeout(taptimeout)
if (swipetimeout) cleartimeout(swipetimeout)
if (longtaptimeout) cleartimeout(longtaptimeout)
touchtimeout = taptimeout = swipetimeout = longtaptimeout = null
touch = {}
}
function isprimarytouch(event){
return (event.pointertype == 'touch' ||
event.pointertype == event.mspointer_type_touch)
&& event.isprimary
}
function ispointereventtype(e, type){
return (e.type == 'pointer'+type ||
e.type.tolowercase() == 'mspointer'+type)
}
$(document).ready(function(){
var now, delta, deltax = 0, deltay = 0, firsttouch, _ispointertype
if ('msgesture' in window) {
gesture = new msgesture()
gesture.target = document.body
}
$(document)
.bind('msgestureend', function(e){
var swipedirectionfromvelocity =
e.velocityx > 1 ? 'right' : e.velocityx 3c9a03a247ac0393497d0514ab814378 1 ? 'down' : e.velocityy dd26010a0e5f1c02454e5e6478d312b7 0 && delta 459f468bcee9ffacf02f4f7ddd59d93c 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
})
// when the browser window loses focus,
// for example when a modal dialog is shown,
// cancel all ongoing events
.on('touchcancel mspointercancel pointercancel', cancelall)
// scrolling the window indicates intention of the user
// to scroll, not tap or swipe, so cancel all ongoing events
$(window).on('scroll', cancelall)
})
;['swipe', 'swipeleft', 'swiperight', 'swipeup', 'swipedown',
'doubletap', 'tap', 'singletap', 'longtap'].foreach(function(eventname){
$.fn[eventname] = function(callback){ return this.on(eventname, callback) }
})
})(zepto)
详细分析:
根据zepto源码,我们很清楚地知道tap事件是通过绑定在document上的touch事件来模拟的。所以用户在点击tap事件(touchstart、touchend)时需要冒泡到document上才会触发。然而用户在touchstart和touchend时会触发click事件,但是此时click事件处于延时300ms,如果在这300ms之内tap事件已经完成,将上层元素删除或隐藏。在300ms到来之际,根据click事件的原则(当click事件的元素处于最上层时会处于click事件,所以有的时候错误的z-index的设置导致无法触发click事件),下层事件被执行,出现穿透现象。让下层是input元素,即使没有绑定click事件,由于其默认聚焦弹出键盘,穿透现象尤为严重。
解决方案:
1、github上有个fastclick插件,用来规避click事件的延时执行。引入文件后添加如下代码,并用click替代可能会导致穿透的tap事件元素。
$(function(){ new fastclick(document.body); })
2、监听touchend事件来替代tap,或者touchstart,并阻止冒泡
$("#close").on("touchend",function(e){ $("#alertbox").hide(); e.preventdefault(); });
3、使用css3的pointer-events : true 和 pointer-events : none交替使用对下层元素设置,阻止触发click事件。
4、延时消失上层元素,使得无法触发下层click事件,尽量在延时350ms以上(本人在ios9.2上微信6.3.15上测试过)。不过这样稍微有些体验不好,我们可以使用css3过度来改善体验。
settimeout(function(){ $(#alertbox).hide(); } , 350 );
5、终极方案:用click替代所有tap。由于click的延时,导致体验问题,最好加上fastclick插件。
下面是我写了个简单的例子:可以用手机访问http://property.pingan.com/app/test/jltest/tap-through.html?a=1
通过例子,我们可以很明显的看到事件穿透后底层的button有按下的效果。在频繁的测试过程中,由于微信会缓存页面导致无法看到即时修改的内容,我们可以通过给url增加一些没用的参数如a=1,这样浏览器就会重新加载。
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0"> <title>test-tap-through</title> <script src="js/zepto.min.js" charset="utf-8"></script> <style media="screen"> body{ margin: 0; padding: 0; } .test1,.test2{ position: relative; } .button{ width: 90%; height: 75px; background-color: #00ffff; margin: 5%; line-height: 75px; text-align: center; font-size: 40px; } .box{ position: absolute; top:0; left: 0; width: 50%; height: 200px; background-color: #ff00ff; margin: 5%; line-height: 100px; text-align: center; font-size: 40px; z-index: 100; } </style> </head> <body> <p> <input type="button" id="button1" value="button1"> <input type="button" id="button2" value="button2"> <p id="box1" style="display:none">box1</p> <p id="box2" style="display:none">box2</p> </p> <p> <input type="button" id="button3" value="button3"> <input type="button" id="button4" value="button4"> <p id="box3" style="display:none">box3</p> <p id="box4" style="display:none">box4</p> </p> </body> <script type="text/javascript"> $("#button1").click(function(){ $("#box2").hide(); $("#box1").show(); }); $("#button2").click(function(){ $("#box1").hide(); $("#box2").show(); }); $("#box2").tap(function(){ $("#box2").hide(); }); $("#box1").tap(function(){ $("#box1").hide(); }); $("#button3").click(function(){ $("#box4").hide(); $("#box3").show(); }); $("#button4").click(function(){ $("#box3").hide(); $("#box4").show(); }); $("#box3").tap(function(){ settimeout(function(){$("#box3").hide();},350); }); $("#box4").tap(function(){ settimeout(function(){$("#box4").hide();},350); }); </script> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
如何访问js的对象属性与方法
如何使用源生css3实现圆环加载进度条
以上就是如何使用zepto tap事件的穿透与点透(附代码)的详细内容。
其它类似信息

推荐信息