HTML5滑动(swipe)事件
移动H5开发中经常用到滑动效果(页面上移、下移、向左滑动、向右滑动等),浏览器并没有内置swipe事件,可以通过touch事件(touchstart、touchmove和touchend)模拟swipe效果。jQuery mobile和zeptojs提供了swipe事件。jquery mobile只有swipeLeft和swipeRight,zeptojs提供了完整的tap和swipe事件。
/**
* @author accountwcx@qq.com
* http://git.oschina.net/accountwcx/rhui
*
* swipe事件,包括swipeLeft、swipeRight、swipeUp、swipeDown。
* 调用方法
* Rhui.mobile.swipeLeft(el, callback, options)
* Rhui.mobile.swipeRight(el, callback, options)
* Rhui.mobile.swipeUp(el, callback, options)
* Rhui.mobile.swipeDown(el, callback, options)
* 如果使用jQuery,调用方法
* $(el).rhuiSwipe('swipeLeft', callback, options);
* $(el).rhuiSwipe('swipeRight', callback, options);
* $(el).rhuiSwipe('swipeUp', callback, options);
* $(el).rhuiSwipe('swipeDown', callback, options);
*/
(function(window, $){
var Rhui = window.Rhui || {};
window.Rhui = Rhui;
Rhui.mobile = (function(){
var touch = {
distance: 30, //滑动距离,超过该距离触发swipe事件,单位像素。
duration: 1000 //滑动时长,超过该时间不触发swipe,单位毫秒。
}; /**
* 绑定事件
* @param el 触发事件的元素
* @param swipe 事件名称,可选值为swipeLeft,swipeRight,swipeUp,swipeDown
* @param callback 事件回调函数
* @param isStopPropagation 是否停止冒泡,true为停止冒泡
* @param isPreventDefault 是否阻止默认事件,true为阻止默认事件
* @param triggerOnMove swipe事件有两种触发方式,一种是在touchmove过程中,只要满足滑动距离条件即触发。
* 一种是在touchend中,进入滑动距离判断,如果满足滑动距离触发。
* 默认是在touchend中触发。
*/
function bindSwipe(el, swipe, callback, triggerOnMove, isStopPropagation, isPreventDefault){
var startPoint, endPoint, timer; /**
* 计算滑动方向
* 首先根据x方向和y方向滑动的长度决定触发x方向还是y方向的事件。
* 然后再判断具体的滑动方向。
* 如果滑动距离不够长,不判断方向。
*/
function swipeDirection(x1, y1, x2, y2){
var diffX = x1 - x2,
diffY = y1 - y2,
absX = Math.abs(diffX),
absY = Math.abs(diffY),
swipe; if(absX >= absY){
if(absX >= touch.distance){
swipe = diffX > 0 ? 'swipeLeft' : 'swipeRight';
}
}else{
if(absY >= touch.distance){
swipe = diffY > 0 ? 'swipeUp' : 'swipeDown';
}
} return swipe;
} // 清除本次滑动数据
function clearSwipe(){
startPoint = undefined;
endPoint = undefined; if(timer !== undefined){
clearTimeout(timer);
timer = undefined;
}
} /**
* 判断是否符合条件,如果符合条件就执行swipe事件
* @param el {HTMLElement} 元素
* @param event {Event} Touch原始事件
* @param return 如果执行了事件,就返回true。
*/
function execSwipe(el, event){
if(startPoint && endPoint && swipeDirection(startPoint.x, startPoint.y, endPoint.x, endPoint.y) === swipe){
callback.call(el, event);
return true;
}
} el.addEventListener('touchstart', function(event){
var self = this, touchPoint = event.touches[0]; if(isStopPropagation){
event.stopPropagation();
} if(isPreventDefault){
event.preventDefault();
} startPoint = {
x: Math.floor(touchPoint.clientX),
y: Math.floor(touchPoint.clientY)
}; timer = setTimeout(function(){
//如果超时,清空本次touch数据
clearSwipe();
}, touch.duration);
}); el.addEventListener('touchmove', function(event){
var self = this, touchPoint = event.touches[0]; if(isStopPropagation){
event.stopPropagation();
} if(isPreventDefault){
event.preventDefault();
} if(startPoint){
endPoint = {
x: Math.floor(touchPoint.clientX),
y: Math.floor(touchPoint.clientY)
}; //执行swipe事件判断,是否符合触发事件
if(triggerOnMove){
if(execSwipe(self, event)){
clearSwipe();
}
}
}
}); el.addEventListener('touchend', function(event){
if(isStopPropagation){
event.stopPropagation();
} if(isPreventDefault){
event.preventDefault();
} execSwipe(self, event);
//清除本次touch数据
clearSwipe();
});
} /**
* @param el {HTMLElement} HTML元素
* @param callback {Function} 事件回调函数
* @param options {Object} 可选参数
* isStopPropagation {Boolean} 是否停止冒泡,true为停止冒泡
* isPreventDefault {Boolean} 是否阻止默认事件,true为阻止默认事件
* triggerOnMove {Boolean}
* swipe事件有两种触发方式,一种是在touchmove过程中,只要满足滑动距离条件即触发。
* 一种是在touchend中,进入滑动距离判断,如果满足滑动距离触发。
* 默认值为false,在touchend中触发。
*/
touch.swipeLeft = function(el, callback, options){
if(options){
bindSwipe(el, 'swipeLeft', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);
}else{
bindSwipe(el, 'swipeLeft', callback);
} }; touch.swipeRight = function(el, callback, options){
if(options){
bindSwipe(el, 'swipeRight', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);
}else{
bindSwipe(el, 'swipeRight', callback);
}
}; touch.swipeUp = function(el, callback, options){
if(options){
bindSwipe(el, 'swipeUp', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);
}else{
bindSwipe(el, 'swipeUp', callback);
}
}; touch.swipeDown = function(el, callback, options){
if(options){
bindSwipe(el, 'swipeDown', callback, options.triggerOnMove, options.isStopPropagation, options.isPreventDefault);
}else{
bindSwipe(el, 'swipeDown', callback);
}
}; return touch;
})(); // 注册jquery方法
if($ && $.fn){
$.fn.extend({
/**
* 模拟touch swipe事件,支持链式调用。
* @param name {String} swipe事件名称,值有swipLeft、swipeRight、swipeUp、swipeDown。
* @param callback {Function} swipe事件回调函数
* @param opts {Object} 可选参数
* isStopPropagation {Boolean} 是否停止冒泡,true为停止冒泡
* isPreventDefault {Boolean} 是否阻止默认事件,true为阻止默认事件
* triggerOnMove {Boolean} swipe事件有两种触发方式,一种是在touchmove过程中,只要满足滑动距离条件即触发。
* 一种是在touchend中,进入滑动距离判断,如果满足滑动距离触发。
* 默认值为false,在touchend中触发。
*/
rhuiSwipe: function(name, callback, opts){
var fnSwipe = Rhui.mobile[name]; if(this.length > 0 && fnSwipe){
this.each(function(){
fnSwipe(this, callback, opts);
});
} return this;
}
});
}
})(window, $);
使用实例:
<style type="text/css">
.test{
width: 400px;
height: 400px;
}
</style>
<div id="div1" class="test"></div>
<div class="test"></div> <script type="text/javascript">
Rhui.mobile.swipeUp(document.getElementById('div1'), function(event){
console.log(event);
}, {
// 可选参数
isStopPropagation: true,
isPreventDefault: true,
triggerOnMove: true
}); $('.test').rhuiSwipe('swipeLeft', function(event){
console.log(event);
}, {
// 可选参数
isStopPropagation: true,
isPreventDefault: true,
triggerOnMove: true
});
</script>
实例展示:
<style type="text/css">
.test{
width: 400px;
height: 400px;
}
</style>
<div id="div1" class="test"></div>
<div class="test"></div> <script type="text/javascript">
Rhui.mobile.swipeUp(document.getElementById('div1'), function(event){
console.log(event);
}, {
// 可选参数
isStopPropagation: true,
isPreventDefault: true,
triggerOnMove: true
}); $('.test').rhuiSwipe('swipeLeft', function(event){
console.log(event);
}, {
// 可选参数
isStopPropagation: true,
isPreventDefault: true,
triggerOnMove: true
});
</script>
zeptojs touch事件
zeptojs也提供了滑动事件,该滑动事件需要引用额外的touch.js。
事件 | 描述 |
---|---|
tap | 类似PC端浏览器的鼠标点击事件,由于移动浏览器点击事件有延迟,tap提供了无延迟的点击效果。 |
singleTap | 效果和tap一样 |
doubleTap | 类似PC端浏览器的鼠标双击事件 |
longTap | 长按事件,在元素上长按超过0.75秒触发。有些浏览器有默认的长按事件,可能会被覆盖。 |
swipe | 滑动事件,该事件不考虑滑动方向。 |
swipeLeft | 向左滑动 |
swipeRight | 向右滑动 |
swipeUp | 向上滑动 |
swipeDown | 向下滑动 |
<style>.delete { display: none; }</style> <ul id=items>
<li>List item 1 <span class=delete>DELETE</span></li>
<li>List item 2 <span class=delete>DELETE</span></li>
</ul> <script>
// show delete buttons on swipe
$('#items li').swipe(function(){
$('.delete').hide()
$('.delete', this).show()
}) // delete row on tapping delete button
$('.delete').tap(function(){
$(this).parent('li').remove()
})
</script>
本文摘自死神的丧钟 http://blog.csdn.net/accountwcx/article/details/49334091
HTML5滑动(swipe)事件的更多相关文章
- 移动端 之 触摸事件、Tap事件和swipe事件
触摸事件 touch是一个事件组,意思不止一个事件,是移动端滑动事件组,touchstart touchmove touchend touchcancel touchstart 当刚刚触摸屏幕的时候触 ...
- H5案例分享:html5重力感应事件
html5重力感应事件 一.手机重力感应图形分析 1.设备围绕z轴的旋转角度为α,α角度的取值范围在[0,360). 设备在初始位置,与地球(XYZ)和身体(XYZ)某个位置对齐. 设备围绕z轴的旋转 ...
- Zepto中的Swipe事件失效
需要阻止浏览器默认滑动的事件 document.addEventListener('touchmove', function (event) { event.preventDefault(); }, ...
- html5重力感应事件之DeviceMotionEvent
前言 今天主要介绍一下html5重力感应事件之DeviceMotionEvent,之前我的一篇文章http://www.haorooms.com/post/jquery_jGestures, 介绍了第 ...
- HTML5服务器发送事件(Server-Send Events)
HTML5服务器发送事件是允许获得来自服务器的更新. server-sent事件-单向传递消息,表示网页自动获取来自服务器的更新. 其中有一个重要的对象,eventsource对象是用来接收服务器发送 ...
- HTML5 服务器发送事件(Server-Sent Events)
沈阳SEO:HTML5 服务器发送事件(server-sent event)允许网页获得来自服务器的更新. Server-Sent 事件 - 单向消息传递 Server-Sent 事件指的是网页自动获 ...
- HTML5: HTML5 服务器发送事件(Server-Sent Events)
ylbtech-HTML5: HTML5 服务器发送事件(Server-Sent Events) 1.返回顶部 1. HTML5 服务器发送事件(Server-Sent Events) HTML5 服 ...
- js监听网页页面滑动滚动事件,实现导航栏自动显示或隐藏
/** * 页面滑动滚动事件 * @param e *///0为隐藏,1为显示var s = 1;function scrollFunc(e) { // e存在就用e不存在就用windon.event ...
- html5滑动事件代码
$(".header").on("touchstart", function(e) { // 判断默认行为是否可以被禁用 if (e.cancelable) { ...
随机推荐
- mockjs模拟前后端交互
mockjs是用于mock数据(造假数据)的组件. mockjs官网链接为:http://mockjs.com/:mockjs官网有mockjs的源代码.API以及示例. mockjs拦截ajax请求 ...
- Webform Application、ViewState
Application(全局对象) Application对象生存期和Web应用程序生存期一样长,生存期从Web应用程序网页被访问开始,HttpApplication类对象Application被自动 ...
- opencv的学习笔记5
总结原博文中的一些边缘检测算子和滤波器.(Canny算子, Sobel算子, Laplace算子以及Scharr滤波器) 首先,一般的边缘检测包括三个步骤: 1)滤波:边缘检测的算法主要是基于图像 ...
- Qt之布局管理--基本布局
Qt提供的布局类以及他们之间的继承关系QLayout-----QGirdLayout | ---QBoxLayout----QHBoxLayout | --QVBoxLayout----------- ...
- 如何写出优雅的Python(二)
Print sorted list: colors=['red','green','blue','yellow'] for color in sorted(colors): print color f ...
- Server.MapPath和Request.PhysicalApplicationPath的异同
很多人对它们都不陌生,在众多的WEB程序中,使用Server.MapPath和Request.PhysicalApplicationPath来操作目录/文件的几率参半,我曾经也经常混用,然而时间久了. ...
- 【转载】调试利器 autoexp.dat
转载:http://www.cppblog.com/flyinghare/archive/2010/09/27/127836.html autoexp.dat入门(调试时自定义变量显示) VC在调试状 ...
- PHP就业班心得:PHP的基本含义与功能特点
PHP的含义 PHP的原始含义:Personal Home Page 个人主页 最开始是加拿大的一哥们,开发了一个在线脚本工具,用来记录有多少人访问过他的在线简历,很受欢迎,于是他就开始开发自己的工 ...
- qt qml scrollbar 移动APP风格的滚动轴
依附于Flickable组件的滚动轴 自动放置在恰当位置 拖动时显示,不动时消失Lisence: MIT 请保留此声明Author: surfsky.cnblogs.com 2014-12 ...
- (转)[BetterExplained]为什么你应该(从现在开始就)写博客
(一)为什么你应该(从现在开始就)写博客 用一句话来说就是,写一个博客有很多好处,却没有任何明显的坏处.(阿灵顿的情况属于例外,而非常态,就像不能拿抽烟活到一百岁的英国老太太的个例来反驳抽烟对健康的极 ...