移动端事件touchstart、touchmove、touchend
关于这三个移动端的事件,详细的资料网上一搜一大片,我就不浪费时间了
1.移动端长按事件
var timer = null;
$(ele).on('touchstart',function(){
timer = setTimeout(function(){
alert("我是长按事件!")
},800);
});
$(ele).on('touchend',function(){
clearTimeout(timer);
});
说明:通过定时器模拟长按事件,这个例子基于jQuery,【ele】是要长按的元素;
2.移动端上下左右滑动事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<title>touch</title>
</head>
<body>
<div id="touchLR">动起来</div>
<script>
// 左右滑动事件
var startX = 0, startY = 0;
function touchSatrtFunc(evt) {
try
{
evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等
var touch = evt.touches[0]; //获取第一个触点
var x = Number(touch.pageX); //页面触点X坐标
var y = Number(touch.pageY); //页面触点Y坐标
//记录触点初始位置
startX = x;
startY = y;
}
catch (e) {
alert('touchSatrtFunc:' + e.message);
}
}
function touchMoveFunc(evt) {
try
{
evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等
var touch = evt.touches[0]; //获取第一个触点
var x = Number(touch.pageX); //页面触点X坐标
var y = Number(touch.pageY); //页面触点Y坐标 var text;
//判断滑动左右方向
if (x - startX>=30) {
text = '向右滑动';
document.getElementById('touchLR').innerHTML = text;
}else if(x - startX<=-30){
text = '向左滑动';
document.getElementById('touchLR').innerHTML = text;
}
//判断滑动上下方向
if (y - startY>=30) {
text = '向下滑动';
document.getElementById('touchLR').innerHTML = text;
}else if(y - startY<=-30){
text = '向上滑动';
document.getElementById('touchLR').innerHTML = text;
}
}
catch (e) {
alert('touchMoveFunc:' + e.message);
}
}
function bindEvent() {
document.addEventListener('touchstart', touchSatrtFunc, false);
document.addEventListener('touchmove', touchMoveFunc, false);
}
bindEvent();
</script>
</body>
</html>
说明:具体原理一搜一堆,这里的具体例子,拷贝就能用;我的学习方式是,不管什么原理之类的,先要做的就是把需求弄出来,在捉摸原理;事半功倍
移动端事件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
近段时间使用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" ...
随机推荐
- PackageManager使用
参考:http://www.linuxidc.com/Linux/2012-02/53072.htm Android系统为我们提供了很多服务管理类,包括ActivityManager.PowerMan ...
- mysql命令详解
mysqld.exe 和 mysql.exe 有什么区别? mysqld.exe 是MySQL后台程序(即MySQL服务器).要想使用客户端程序,该程序必须运行,因为客户端通过连接服务器来访问数据库. ...
- [Node.js] Node.js中的流
原文地址:http://www.moye.me/2015/03/29/streaming_in_node/ 什么是流? 说到流,就涉及到一个*nix的概念:管道——在*nix中,流在Shell中被实现 ...
- QT学习笔记1
不准备用MFC了,想切换到QT.所以跟着网上的一个笔记学习. 1 开发环境是VS2008+QT4.7+VassistX 具体如何配置看这个帖子:http://qimo601.iteye.com/blo ...
- QWebView在 Qt 5.x中编译出错:File not found: main.obj
错误现象 近日由于项目需要,想要学习一下QWebView的使用.于是简单的建立了一个Qt工程,并编写了如下代码: #include <QApplication> #include < ...
- 在Winform开发中使用日程控件XtraScheduler(2)--深入理解数据的存储
在上篇随笔<在Winform开发中使用日程控件XtraScheduler>中介绍了DevExpress的XtraScheduler日程控件的各种使用知识点,对于我们来说,日程控件不陌生,如 ...
- iOS 学习笔记二【cocopods安装使用和安装过程中遇到的问题及解决办法】【20160725更新】
在osx 10.11之前cocopods问题不多,但是升级到11之后的版本,之前的cocopods大多用不了,需要重新安装,对于我这种使用测试版系统的技术狂来说,每次都需要重新安装很多东西, 当然,c ...
- 孙鑫MFC学习笔记6:菜单编程
1.对菜单响应的顺序: 视类,文档类,框架类,应用程序类 2.消息的分类 3.CWnd继承自CCmdTarget类, 所以从CWnd派生出的类也可以接收WM_COMMAND消息 4.命令的消息路由 5 ...
- mysql中变量赋值
http://www.cnblogs.com/qixuejia/archive/2010/12/21/1913203.html sql server中变量要先申明后赋值: 局部变量用一个@标识,全局变 ...
- [转]JavaScript程序编码规范
原文:http://javascript.crockford.com/code.html 作者:Douglas Crockford 译文:http://www.yeeyan.com/articles/ ...