有时,当你只想为触屏划动添加事件时,很多人可能会想到,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. Div内部的内容超出部分显示省略号(仅仅只有一行内容)

    效果如下:

  2. iOS应用架构谈(三):View层的组织和调用方案(下)

    iOS客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答iOS应用架构中的种种问题,本文是其中的第二篇,主要讲View层的组织和调用方案.下篇主要讨论做View层架构的 ...

  3. Mysql 死锁的详细分析方法

    用数据库的时候,偶尔会出现死锁,针对我们的业务系统,出现死锁的直接结果就是系统卡顿.客户找事儿,所以我们也在想尽全力的消除掉数据库的死锁.出现死锁的时候,如果只是想解锁,用show full proc ...

  4. Mac与iPhone屏幕录制

    1. Mac电脑屏幕录制 1.1 文件->新建屏幕录制   1.2 点击红色按钮   1.3 截取需要录制的屏幕部分,点击开始录制   1.4 点击工具栏的停止按钮,停止录制   1.5 然后会 ...

  5. IE6中使用通用选择器模拟子选择器效果

    IE6及更低版本不支持高级选择器:IE7有个bug,对于子选择器和相邻同胞选择器,如果父元素和子元素有HTML注释,会出问题. 下面我们使用通用选择器来模拟子选择器的效果. 原理:首先在所有后代上应用 ...

  6. 3.2 STL中的函数对象类模板

    *: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...

  7. 20145206《Java程序设计》第10周学习总结

    20145206 <Java程序设计>第10周学习总结 博客学习内容总结 什么是网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定 ...

  8. Juery Ajax语法

    $.ajax({ url: "/ForgetCard/ForgetLogin",//方法路径URL data: { strUser: $("#textUser" ...

  9. 攻城狮在路上(叁)Linux(二十八)--- 打包命令:tar

    首先介绍一下tar打包命令的基本格式,下面的三种之间不能混淆. tar [-j|-z] [cv] [-f 新文件名] file1 file2 ...; tar [-j|-z] [tv] [-f 新文件 ...

  10. 团队作业-第一周-NABCD竞争性需求分析

      1.  Need 需求 随着科技信息的发展,传统的课堂点名亟待步入信息处理的轨道,移动校园课堂点名软件恰好的切入了这个需求点,市场中词类软件也为数不多,因此需求也是比较强烈. 2. Approac ...