移动端touchstart,touchmove,touchend
近段时间使用html5开发一个公司内部应用,而触摸事件必然是移动应用中所必须的,刚开始以为移动设备上或许也会支持鼠标事件,原来是不支持的,好在webkit内核的移动浏览器支持touch事件,并且打包成app也毫无压力。原本以为touch事件应该跟鼠标事件是一样的道理,实践过程中虽然不难,但还是碰到了不少坑,才发现还是略有区别的。
$(document).bind(touchEvents.touchstart, function (event) {
event.preventDefault();
});
$(document).bind(touchEvents.touchmove, function (event) {
event.preventDefault();
});
$(document).bind(touchEvents.touchend, function (event) {
event.preventDefault();
});
很多博文中称touch的三个事件都有targetTouches,touches以及changedTouches对象列表,其实不然,touchend事件中应该是只有个changedTouches触摸实例列表的,而且这里说明一下,回调函数的event只是一个普通的object对象,实际上event中有一个originalEvent属性,这才是真正的touch事件,这个事件中才存在着上诉三个触摸实例列表,这三个实例存储了触摸事件的位置等等属性,类似于鼠标事件。其他地方基本与鼠标事件是一致的。简单介绍一下这三个触摸列表,touches是在屏幕上的所有手指列表,targetTouches是当前DOM上的手指列表,所以当手指移开触发touchend事件时,event.originalEvent是没有这个targetTouches列表的,而changedTouches列表是涉及当前事件的列表,例如touchend事件中,手指移开。接下来谈谈pc与移动端的适配问题,既然使用html5,当然是看中他的跨平台特性了,不仅仅要ios和android适配,pc上直接打开网页最好也是可以的,但是pc上只支持鼠标事件怎么办。好办,仔细观察上面代码的触摸事件,touchEvents.touchXXX,看如下代码:
var touchEvents = {
touchstart: "touchstart",
touchmove: "touchmove",
touchend: "touchend",
/**
* @desc:判断是否pc设备,若是pc,需要更改touch事件为鼠标事件,否则默认触摸事件
*/
initTouchEvents: function () {
if (isPC()) {
this.touchstart = "mousedown";
this.touchmove = "mousemove";
this.touchend = "mouseup";
}
}
};
若在pc上,则使用鼠标事件,在移动设备中,就使用触摸事件,就这么简单,判断是否pc也很方便,就不做多解释了。
demo
var praiseBtn=document.querySelector('.praiseBtn');//获取div
praiseBtn.addEventListener("touchstart",StartPraiseBtn,false);//添加触摸事件
praiseBtn.addEventListener("touchend",StopPraiseBtn,false);//添加触摸事件
//touchstart
function StartPraiseBtn()
{
document.querySelector('.praiseBtn').className="praiseBtn current";//添加open类
}
//touchend
function StopPraiseBtn()
{
document.querySelector('.praiseBtn').className="praiseBtn current";
}
移动端touchstart,touchmove,touchend的更多相关文章
- 移动端的touchstart,touchmove,touchend事件中的获取当前touch位置
前提:touchstart,touchmove,touchend这三个事件可以通过原生和jq绑定. 原生:document.querySelector("#aa").addEven ...
- 获取touchstart,touchmove,touchend 坐标
简单说下如何用jQuery 和 js原生代码获取touchstart,touchmove,touchend 坐标值: jQuery 代码: $('#id').on('touchstart',funct ...
- touchstart,touchmove,touchend触摸事件的小小实践心得
近段时间使用html5开发一个公司内部应用,而触摸事件必然是移动应用中所必须的,刚开始以为移动设备上或许也会支持鼠标事件,原来是不支持的,好在webkit内核的移动浏览器支持touch事件,并且打包成 ...
- 手机端touchstart,touchmove,touchend事件,优化用户划入某个可以点击LI的效果
在我们滑动手机的时候,如果LI或者DIV标签可以点击,那么在移动端给用户一个效果 /*id为添加效果LI上的UL的ID,或者是当前DIV的ID*/ function doTouchPublic(id) ...
- touchstart,touchmove,touchend事件 写法
jQuery写法: $('#id').on('touchstart',function(e) { var _touch = e.originalEvent.targetTouches[0]; var ...
- JQuery 获取touchstart,touchmove,touchend 坐标
JQuery写法: $('#id').on('touchstart',function(e) { var _touch = e.originalEvent.targetTouches[0]; var ...
- jQuery的touchstart,touchmove,touchend的获取位置
$('#webchat_scroller').on('touchstart',function(e) { var touch = e.originalEvent.targetTouches[0]; v ...
- JQuery获取touchstart,touchmove,touchend坐标
$('#id').on('touchstart',function(e) { ].pageX; }); JQuery如上. document.getElementById("id" ...
- 解决移动端touch事件(touchstart/touchend) 的穿透问题
情景: 我们在移动端点击事件click对比touchend会有很明显的300ms的延迟,为啥? 浏览器在 click 后会等待约300ms去判断用户是否有双击行为(手机需要知道你是不是想双击放大网页内 ...
随机推荐
- Debian Security Advisory(Debian安全报告) DSA-4411-1 firefox-esr security update
Debian Security Advisory(Debian安全报告) DSA-4411-1 firefox-esr security update Package :firefox-esr CV ...
- [C++]动态规划系列之币值最大化
/** * * @author Zen Johnny * @date 2018年3月31日 下午10:04:48 * */ package freeTest.dynamicProgramming; i ...
- python 的基础 学习 第一天
1 python 的变量 1,变量必须 由数字,字母和下划线组成 2,变量不能由数字开头,例如 :22hhh , 3,变量不能是由Python中的关键字组成. 4,变量具有可描述性,不易过长. 5,变 ...
- linux C遍历目录下文件
参考链接: http://blog.sina.com.cn/s/blog_626b7339010161tr.html
- java.lang.Enum
参考博客链接 java枚举类型的优势在哪里? Java 枚举(enum) 详解7种常见的用法
- whatis命令,一个非常有用的命令
比如我不知道yum的配置文件yum.conf怎么用,就可以whatis yum.conf,
- 第六天-request response\04-response实现文件下载.avi--本人测试失败
package cn.itcast.response; import java.io.FileInputStream;import java.io.IOException;import java.io ...
- 调整JVM虚拟机的内存大小
jvm默认只有64M; public static void main(String[] args){ byte b[] = new byte[1024*1024*65];//此时会报内存溢出: } ...
- redis-set
Java HashSet 无序,不重复. Redis操作中,涉及到两个大数据集合的并集,交集,差集运算. 赋值: sadd key values[value1.value2…]:向set中添加数据, ...
- 和类相关的BIF
issubclass(class,classinfo),判断第一个参数是第二个参数的子类,这个判断是比较宽松的判断.因为第二个参数也可以是元组,并且只要参数1是元组内任意一个元素的子元素,就会返回Tr ...