有时,当你只想为触屏划动添加事件时,很多人可能会想到,Jquery mobile,但就这么个功能就把人家这么高大上的东西引用进来就有点大才小用了,WipeTouch是国外某程序员写的针对触屏划动的jquery 插件,足够用了

// jQuery WipeTouch 1.2.0
// ------------------------------------------------------------------------
//
// Developed and maintained by Igor Ramadas
// http://aboutigor.com
// http://devv.com
//
// USAGE
// ------------------------------------------------------------------------
//
// $(selector).wipetouch(config);
//
// The wipe events should expect the result object with the following properties:
// speed - the wipe speed from 1 to 5
// x - how many pixels moved on the horizontal axis
// y - how many pixels moved on the vertical axis
// source - the element which triggered the wipe gesture
//
// EXAMPLE
// $(document).wipetouch({
// allowDiagonal: true,
// wipeLeft: function(result) { alert("Left on speed " + result.speed) },
// wipeTopLeft: function(result) { alert("Top left on speed " + result.speed) },
// wipeBottomLeft: function(result) { alert("Bottom left on speed " + result.speed) }
// });
//
//
// More details at http://wipetouch.codeplex.com/
//
// CHANGE LOG
// ------------------------------------------------------------------------
// 1.2.0
// - New: wipeMove event, triggered while moving the mouse/finger.
// - New: added "source" to the result object.
// - Bug fix: sometimes vertical wipe events would not trigger correctly.
// - Bug fix: improved tapToClick handler.
// - General code refactoring.
// - Windows Phone 7 is not supported, yet! Its behaviour is completely broken and would require some special tricks to make it work. Maybe in the future...
//
// 1.1.0
// - New: tapToClick, if true will identify taps and and trigger a click on the touched element. Default is false.
// - Changed: events wipeBottom*** and wipeTop*** renamed to wipeDown*** and wipeUp***.
// - Changed: better touch speed calculation (was always too fast before).
// - Changed: speed will be an integer now (instead of float).
// - Changed: better wipe detection (if Y movement is more than X, do a vertical wipe instead of horizontal).
// - Bug fix: added preventDefault to touchStart and touchEnd internal events (this was missing).
// - Other general tweaks to the code.
//
// The minified version of WipeTouch can be generated using Jasc: http://jasc.codeplex.com (function ($)
{
$.fn.wipetouch = function (settings)
{
// ------------------------------------------------------------------------
// PLUGIN SETTINGS
// ------------------------------------------------------------------------ var config = { // Variables and options
moveX: 40, // minimum amount of horizontal pixels to trigger a wipe event
moveY: 40, // minimum amount of vertical pixels to trigger a wipe event
tapToClick: false, // if user taps the screen it will fire a click event on the touched element
preventDefault: true, // if true, prevents default events (click for example)
allowDiagonal: false, // if false, will trigger horizontal and vertical movements so wipeUpLeft, wipeDownLeft, wipeUpRight, wipeDownRight are ignored // Wipe events
wipeLeft: false, // called on wipe left gesture
wipeRight: false, // called on wipe right gesture
wipeUp: false, // called on wipe up gesture
wipeDown: false, // called on wipe down gesture
wipeUpLeft: false, // called on wipe top and left gesture
wipeDownLeft: false, // called on wipe bottom and left gesture
wipeUpRight: false, // called on wipe top and right gesture
wipeDownRight: false, // called on wipe bottom and right gesture
wipeMove: false, // triggered whenever touchMove acts // DEPRECATED EVENTS
wipeTopLeft: false, // USE WIPEUPLEFT
wipeBottomLeft: false, // USE WIPEDOWNLEFT
wipeTopRight: false, // USE WIPEUPRIGHT
wipeBottomRight: false // USE WIPEDOWNRIGHT
}; if (settings)
{
$.extend(config, settings);
} this.each(function ()
{
// ------------------------------------------------------------------------
// INTERNAL VARIABLES
// ------------------------------------------------------------------------ var startX; // where touch has started, left
var startY; // where touch has started, top
var startDate = false; // used to calculate timing and aprox. acceleration
var curX; // keeps touch X position while moving on the screen
var curY; // keeps touch Y position while moving on the screen
var isMoving = false; // is user touching and moving?
var touchedElement = false; // element which user has touched // These are for non-touch devices!
var useMouseEvents = false; // force using the mouse events to simulate touch
var clickEvent = false; // holds the click event of the target, when used hasn't clicked // ------------------------------------------------------------------------
// TOUCH EVENTS
// ------------------------------------------------------------------------ // Called when user touches the screen.
function onTouchStart(e)
{
var start = useMouseEvents || (e.originalEvent.touches && e.originalEvent.touches.length > 0); if (!isMoving && start)
{
if (config.preventDefault)
{
e.preventDefault();
} // Temporary fix for deprecated events, these will be removed on next version!
if (config.allowDiagonal)
{
if (!config.wipeDownLeft)
{
config.wipeDownLeft = config.wipeBottomLeft;
} if (!config.wipeDownRight)
{
config.wipeDownRight = config.wipeBottomRight;
} if (!config.wipeUpLeft)
{
config.wipeUpLeft = config.wipeTopLeft;
} if (!config.wipeUpRight)
{
config.wipeUpRight = config.wipeTopRight;
}
} // When touch events are not present, use mouse events.
if (useMouseEvents)
{
startX = e.pageX;
startY = e.pageY; $(this).bind("mousemove", onTouchMove);
$(this).one("mouseup", onTouchEnd);
}
else
{
startX = e.originalEvent.touches[0].pageX;
startY = e.originalEvent.touches[0].pageY; $(this).bind("touchmove", onTouchMove);
} // Set the start date and current X/Y.
startDate = new Date().getTime();
curX = startX;
curY = startY;
isMoving = true; touchedElement = $(e.target);
}
} // Called when user untouches the screen.
function onTouchEnd(e)
{
if (config.preventDefault)
{
e.preventDefault();
} // When touch events are not present, use mouse events.
if (useMouseEvents)
{
$(this).unbind("mousemove", onTouchMove);
}
else
{
$(this).unbind("touchmove", onTouchMove);
} // If is moving then calculate the touch results, otherwise reset it.
if (isMoving)
{
touchCalculate(e);
}
else
{
resetTouch();
}
} // Called when user is touching and moving on the screen.
function onTouchMove(e)
{
if (config.preventDefault)
{
e.preventDefault();
} if (useMouseEvents && !isMoving)
{
onTouchStart(e);
} if (isMoving)
{
if (useMouseEvents)
{
curX = e.pageX;
curY = e.pageY;
}
else
{
curX = e.originalEvent.touches[0].pageX;
curY = e.originalEvent.touches[0].pageY;
} // If there's a wipeMove event, call it passing
// current X and Y position (curX and curY).
if (config.wipeMove)
{
triggerEvent(config.wipeMove, {
curX: curX,
curY: curY
});
}
}
} // ------------------------------------------------------------------------
// CALCULATE TOUCH AND TRIGGER
// ------------------------------------------------------------------------ function touchCalculate(e)
{
var endDate = new Date().getTime(); // current date to calculate timing
var ms = startDate - endDate; // duration of touch in milliseconds var x = curX; // current left position
var y = curY; // current top position
var dx = x - startX; // diff of current left to starting left
var dy = y - startY; // diff of current top to starting top
var ax = Math.abs(dx); // amount of horizontal movement
var ay = Math.abs(dy); // amount of vertical movement // If moved less than 15 pixels, touch duration is less than 100ms,
// and tapToClick is true then trigger a click event and stop processing.
if (ax < 15 && ay < 15 && ms < 100)
{
clickEvent = false; if (config.preventDefault)
{
resetTouch(); touchedElement.trigger("click");
return;
}
}
// When touch events are not present, use mouse events.
else if (useMouseEvents)
{
var evts = touchedElement.data("events"); if (evts)
{
// Save click event to the temp clickEvent variable.
var clicks = evts.click; if (clicks && clicks.length > 0)
{
$.each(clicks, function (i, f)
{
clickEvent = f;
return;
}); touchedElement.unbind("click");
}
}
} // Is it moving to the right or left, top or bottom?
var toright = dx > 0;
var tobottom = dy > 0; // Calculate speed from 1 to 5, 1 being slower and 5 faster.
var s = ((ax + ay) * 60) / ((ms) / 6 * (ms)); if (s < 1) s = 1;
if (s > 5) s = 5; var result = {
speed: parseInt(s),
x: ax,
y: ay,
source: touchedElement
}; if (ax >= config.moveX)
{
// Check if it's allowed and trigger diagonal wipe events.
if (config.allowDiagonal && ay >= config.moveY)
{
if (toright && tobottom)
{
triggerEvent(config.wipeDownRight, result);
}
else if (toright && !tobottom)
{
triggerEvent(config.wipeUpRight, result);
}
else if (!toright && tobottom)
{
triggerEvent(config.wipeDownLeft, result);
}
else
{
triggerEvent(config.wipeUpLeft, result);
}
}
// Otherwise trigger horizontal events if X > Y.
else if (ax >= ay)
{
if (toright)
{
triggerEvent(config.wipeRight, result);
}
else
{
triggerEvent(config.wipeLeft, result);
}
}
}
// If Y > X and no diagonal, trigger vertical events.
else if (ay >= config.moveY && ay > ax)
{
if (tobottom)
{
triggerEvent(config.wipeDown, result);
}
else
{
triggerEvent(config.wipeUp, result);
}
} resetTouch();
} // Resets the cached variables.
function resetTouch()
{
startX = false;
startY = false;
startDate = false;
isMoving = false; // If there's a click event, bind after a few miliseconds.
if (clickEvent)
{
window.setTimeout(function ()
{
touchedElement.bind("click", clickEvent);
clickEvent = false;
}, 50);
}
} // Trigger a wipe event passing a result object with
// speed from 1 to 5, x / y movement amount in pixels,
// and the source element.
function triggerEvent(wipeEvent, result)
{
if (wipeEvent)
{
wipeEvent(result);
}
} // ------------------------------------------------------------------------
// ADD TOUCHSTART AND TOUCHEND EVENT LISTENERS
// ------------------------------------------------------------------------ if ("ontouchstart" in document.documentElement)
{
$(this).bind("touchstart", onTouchStart);
$(this).bind("touchend", onTouchEnd);
}
else
{
useMouseEvents = true; $(this).bind("mousedown", onTouchStart);
$(this).bind("mouseout", onTouchEnd);
}
}); return this;
};
})(jQuery);

jQuery WipeTouch的更多相关文章

  1. jQuery Cycle Plugin的使用

    jQuery幻灯片效果或者Slideshow效果当中如果不考虑touch效果的话,jQuery Cycle插件实在是太强大了,各种高大上的动画效果,如果想加上touch效果可以结合本blog介绍的wi ...

  2. jQuery Multi-TouchWipe / Multi-TouchZoom

    jQuery Multi-TouchWipe / Multi-TouchZoom是小弟参照WipeTouch扩展出来的针对多点触屏划动而改写出来的Jquery插件,可以为dom上的两手指触屏划动拨入( ...

  3. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  4. jQuery UI resizable使用注意事项、实时等比例拉伸及你不知道的技巧

    这篇文章总结的是我在使用resizable插件的过程中,遇到的问题及变通应用的奇思妙想. 一.resizable使用注意事项 以下是我在jsfiddle上写的测试demo:http://jsfiddl ...

  5. Jquery的点击事件,三句代码完成全选事件

    先来看一下Js和Jquery的点击事件 举两个简单的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  6. jQuery实践-网页版2048小游戏

    ▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...

  7. jquery和Js的区别和基础操作

    jqery的语法和js的语法一样,算是把js升级了一下,这两种语法可以一起使用,只不过是用jqery更加方便 一个页面想要使用jqery的话,先要引入一下jqery包,jqery包从网上下一个就可以, ...

  8. jQuery之ajax实现篇

    jQuery的ajax方法非常好用,这么好的东西,你想拥有一个属于自己的ajax么?接下来,我们来自己做一个简单的ajax吧. 实现功能 由于jq中的ajax方法是用了内置的deferred模块,是P ...

  9. 利用snowfall.jquery.js实现爱心满屏飞

    小颖在上一篇一步一步教你用CSS画爱心中已经分享一种画爱心的方法,这次再分享一种方法用css画爱心,并利用snowfall.jquery.js实现爱心满屏飞的效果. 第一步: 利用伪元素before和 ...

随机推荐

  1. Innodb之表空间转移

    我们可以将数据表转移到其他磁盘,以减弱单个磁盘的IO. 如 1创建一个表空间: 2修改表以使用新的表空间,如果表有大量数据,则会需要一些时间重建:所以会锁表一段时间: Note:会将原有的表空间删除, ...

  2. Android touch事件的派发流程

    Android TouchEvent事件传递机制 通俗易懂,能够了解Touch事件派发的基本流程. Android中的dispatchTouchEvent().onInterceptTouchEven ...

  3. route 一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机

    一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机. [root@NB Desktop]# route ...

  4. [插件]jQuery multiselect初始化及默认值修改

    下载地址:http://pan.baidu.com/s/1dE2daSD 1.Jquery多选下拉列表插件jquery multiselect功能介绍及使用 http://www.jb51.net/a ...

  5. 无废话ExtJs 入门教程五[文本框:TextField]

    无废话ExtJs 入门教程五[文本框:TextField] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个两个文本框.如下所示代码区的第42行位置,items: ...

  6. SSAS Cube 维度成员关系Rigid 和 Flexible

    维度成员关系指示成员关系是否随时间而更改.  值为 Rigid 和 Flexible,前者表示成员之间的关系不随时间而更改,后者表示成员之间的关系随时间而更改. 默认值为 Flexible.  指定适 ...

  7. 攻城狮在路上(贰) Spring(一)--- 软件环境、参考书目等一览表

    一.软件环境: 二.参考书目: <Spring 3.X 企业应用开发实战> 陈雄华.林开雄著 电子工业出版社出版 三.其他说明: spring 源码地址:https://github.co ...

  8. Win10 UI线程

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateButtonOrientation());

  9. 重新开始刷dp,哈哈哈

    转载于: http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2015年1月29日 从头开始

  10. zzy:java采用的是16位的Unicode字符集作为编码方式------理解

    java语言使用16位的Unicode字符集作为编码方式,是疯狂Java中的原话. 1,编码方式只是针对字符类型的(不包括字符串类,数值类型int等,这些只是在解释[执行]的时候放到Jvm的不同内存块 ...