移动端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去判断用户是否有双击行为(手机需要知道你是不是想双击放大网页内 ...
 
随机推荐
- Java SE之正则表达式一:概述
			
正则表达式 概念 定义:符合一定规则的表达式 作用:用于专门操作字符串 特点:用于一些特定的符号表示代码的操作,这样就简化了长篇的程序代码 好处:可以简化对字符串的复杂操作 弊端:符号定义越多,正则越 ...
 - 向dnsrecord.txt 中添加 配置
			
#!/bin/bash Ip_addr=`cat /etc/dnsrecord.txt |awk '{print $1}' |head -1` Check_dns_url(){ grep $1 /et ...
 - 写自适应的textarea文本域
			
<div contenteditable="true"> </div> 我们都知道默认的textarea无法自适应,一般情况下都是内容过多里面通过滚动条来滚 ...
 - Failed to read artifact descriptor for org.apache.maven.plugins:maven-install-plugin-JavaWeb(四)
			
今天使用maven clean, maven install 出现了下图问题,只解决了 maven clean , 还有maven install 今天 使用maven clean 出现以下问题(把下 ...
 - P1880 [NOI1995]石子合并(区间DP)
			
题目链接:https://www.luogu.org/problemnew/show/P1880 题目大意:中文题目 具体思路:和上一篇的思路是差不多的,也是对于每一个小的区间进行处理,然后再归并到大 ...
 - DeepLearning.ai-Week4-Deep Learning & Art: Neural Style Transfer
			
1 - Task Implement the neural style transfer algorithm Generate novel artistic images using your alg ...
 - Hadoop Steaming开发之WordCount
			
简单的WordCount栗子--类似于编程语言中的hello world 1.shell脚本run.sh HADOOP_CMD="/usr/local/src/hadoop-1.2.1/bi ...
 - HBSX2019 3月训练
			
Day 1 3月有31天废话 今天先颓过了就只剩30天了 初步计划 每天一道字符串/数据结构题 图论学习 根据<若干图论模型探讨>(lyd)复习 二分图与网络流学习 <算法竞赛进阶指 ...
 - 论文笔记:Selective Search for Object Recognition
			
与 Selective Search 初次见面是在著名的物体检测论文 「Rich feature hierarchies for accurate object detection and seman ...
 - 【运维】Dell R710如何开启VT服务
			
[前言]: 英特尔的硬件辅助虚拟化技术(Virtualization Technology,简称VT技术)是一种设计更简单.实施更高效和可靠的方法. 如果想要在 ...