一:click事件的封装

手机端的click事件不是太好用,

1.点击会有延迟,

2.会出现一个灰色区域

就想到了用touch事件代替.

touch事件有touchstart,touchmove,touchend.

在使用的时候会有这样的一种情况.

现在我想在一个元素上使用touch实现单击.

意思就是当我的手点下去,在抬起来后触发事件.

如果我绑定touchstart肯定是不行的:手还没抬起,就触发了事件.

如果绑定touchend.会有一些不友好的体验.

比如:我的手在元素上滑动,然后抬起,就会触发这个事件.

很明显有些时候我们只需要类似于单击事件.这种手滑动后不需要触发的事件.

下面这个代码就是一个移动端的click事件.

(function(){
$.fn.touchClick=function(callback){
this.each(function(){
var obj=$(this);
obj.on("touchstart",function(){
obj.data("move",false);
}).on("touchmove",function(){
obj.data("move",true);
}).on("touchend",function(event){
if(obj.data("move")){
return;
}else{
            if(typeof callback==="function"){
              callback(obj,event);
            }
}
obj.data("move",false);
});
});
};
})(jQuery);
$("#div").touchClick(function(self,event){
self.hide();
});

二:移动端touch事件的跨页传递.

现在又A,B两个页面.上面各有一个元素#a,#b.在当前页面的同样位置.

$("#a").on("touchend",function(){
window.location.href="B.html";
});
$("#b").on("touchend",function(){
alert("B");
});

点击a之后直接跳转到B.html.但是诡异的是.触发了b元素的touch事件.

解决办法.

$("#a").on("touchend",function(){
window.location.href="B.html";
return false
});

click事件有三个过程.手按下,手滑动,手抬起.

重新封装touchclick,使其可以出发这三个事件:

(function(){
var defaults={
start:function(self,event){},
move:function(self,event){},
end:function(self,event){}
}
$.fn.touchClick1=function(opts){
opts=$.extend({}, defaults,opts);
this.each(function(){
var obj=$(this);
obj.on("touchstart",function(event){
obj.data("move",false);
return opts.start(obj,event);
}).on("touchmove",function(event){
obj.data("move",true);
return opts.move(obj,event);
}).on("touchend",function(event){
if(obj.data("move")){
return;
}else{
return opts.end(obj,event);
}
obj.data("move",false);
});
});
};
})(jQuery);

上面的写法有个弊端,每次想访问当前对象的都得使用self.不太好。如果直接用this不是更好么。改写上面的代码

(function(){
var defaults={
start:function(self,event){},
move:function(self,event){},
end:function(self,event){}
}
$.fn.touchClick=function(opts){
if(typeof opts=="function"){
opts=$.extend({}, defaults,{end:opts});
}else{
opts=$.extend({}, defaults,opts);
}
this.each(function(){
var obj=$(this);
obj.on("touchstart",function(event){
obj.data("move",false);
opts.start.call(this,event);
}).on("touchmove",function(event){
obj.data("move",true);
opts.move.call(this,event);
}).on("touchend",function(event){
if(obj.data("move")){
return;
}else{
opts.end.call(this,event);
}
obj.data("move",false);
});
});
};
})(jQuery);

手机端的click的更多相关文章

  1. h5手机端下拉选择城市

    <!doctype html><html>    <head>            <meta http-equiv="Content-Type& ...

  2. js 判断pc端或手机端

    <script> (function () { var navUA = navigator.userAgent; var defIncludeStr = "iPhone|Andr ...

  3. 手机端的tab切换,响应式切换效果

    之前写过这些tab切换的效果,无论网页上还是手机端,网上也有很多的例子,这个好像是我参考网上,也不知道是哪里的了.总结了一下,就当保存下来了把. <!DOCTYPE html > < ...

  4. 手机端flex、字体设置、快速点击

    ;(function flexible (window, document) { var docEl = document.documentElement ♥1 var dpr = window.de ...

  5. h5 手机端适配问题汇总

    1.uc手机浏览器竟然没有 sessionstorage 醉了 2.opera 浏览器  能识别 a标签中href的  javascript:; 为网址  ,  55555 3.safari 的弹框如 ...

  6. XE5 Android 开发数据访问手机端[转]

    把供手机端调用的web服务完成,接下来实现手机端调用webservices获取数据 1.新建firemonkey mobile application 2.选择blank application 3. ...

  7. 手机端图片预览和缩放js

    转至:http://blog.sina.com.cn/s/blog_c342e3090102vcxu.html 1.手机端的图片选择和预览 <input type="file" ...

  8. HTML5手机端拍照上传

    1.accept="image/*" capture="camera" 自动调用手机端拍照功能 accept="image/*" captu ...

  9. 手机端网页返回顶部js代码

    <!DOCTYPE html>  <html>  <head>  <meta http-equiv="Content-Type" cont ...

随机推荐

  1. wheel和staff分组

    1.只有属于wheel组的用户才可以用su登录为root 2. 具体设置步骤如下: 1)修改 /etc/pam.d/su 文件,找到"#auth required /lib/security ...

  2. hdu 1052 (greedy algorithm) 分类: hdoj 2015-06-18 16:49 35人阅读 评论(0) 收藏

    thanks to http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=19638&m ...

  3. [Python模式]策略模式

    策略模式 定义了算法族,分别封装起来,让它们之间可以互相替换.此模式让算法的变化独立于使用算法的客户. 作为动态语言,Python实现策略模式非常容易,只要所有算法提供相同的函数即可. import ...

  4. a few changes of Android 5.0

    1.Service Intent must be explicit Intent serviceIntent = new Intent(context,MyService.class);context ...

  5. 上下margin重叠传递问题

    我发现强迫症真的是我一个大病...每次都非得把所有情况都实验出来不可...BUT!!!!!!!!!悲催的是,这么多情况我根本记不住...还是要在写代码的时候不断出错再排错~受不了自己了!不过还是把这部 ...

  6. poj 1185 (状压dp)

    Problem 炮兵阵地 题目大意 给你一张n*m的地图,一些地区是空地,一些地区是障碍. 可以在空地上布置炮兵部队,炮兵部队的攻击范围为上下左右各两格. 询问最多可以布置多少个炮兵部队,且互不伤害. ...

  7. linux内核分析——扒开系统调用的三层皮

    万子惠 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验部分 选择2 ...

  8. java 后台校验格式

    package com.hengxin.qianee.utils; import java.net.InetAddress; public class RegexUtils { /** * 用户名是否 ...

  9. 不同vlan之间的相互访问

    拓扑图: 用到的命令: 给端口的vlan <sw1>用户模式 切换到系统模式 system-view 交换机名称 sysname swj1 创建vlan 3 端口模式选择int g0/0/ ...

  10. xmind的第九天笔记