有时,当你只想为触屏划动添加事件时,很多人可能会想到,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. 让U盘永不中毒的解决办法

    一.背景: 在学校上课的时候,有个老师很潇洒的拿着一个U盘就来教室上课了.然后快上课的时候在电脑上准备播放课件.注意,这一瞬间其妙的事情发生了,课件因为他的U盘中病毒了,打不开了,老师当时笑了.后来又 ...

  2. java删除被占用的文件

    boolean result = f.delete();//判断是否删除完毕 if(!result) { System.gc();//系统进行资源强制回收 f.delete; }

  3. jquery.validation.js 表单验证

    jquery.validation.js 表单验证   官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuer ...

  4. 手写代码自动实现自动布局,即Auto Layout的使用

    手写代码自动实现自动布局,即Auto Layout的使用,有需要的朋友可以参考下. 这里要注意几点: 对子视图的约束,若是基于父视图,要通过父视图去添加约束. 对子视图进行自动布局调整,首先对UIVi ...

  5. Generic Access Profile

    转自:https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile ​​Assigned numbe ...

  6. 图结构练习——最小生成树(prim算法(普里姆))

      图结构练习——最小生成树 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  有n个城市,其中有些城市之间可以修建公路,修建不同 ...

  7. SQLAchemy Core学习之Reflection

    如果以后万一有一个定义好了的库,可以用这种反射的方法,作常用的操作. #coding=utf-8 from datetime import datetime from sqlalchemy impor ...

  8. 玩玩Excel下的Power View

    作为微软平台下的数据展示工具,Power View是一个不错的选择.而在Excel 2013下,即使你没有SharePoint的实例那么你也可以玩转它.此篇讲对Excel 2013下的Power Vi ...

  9. foreach与Iterable学习

    以前对于foreach的使用都是自然而然的感觉,没有深究过为什么可以用,什么时候可以用.最近才发现,原来那些可以使用的类,都是实现了Iterable接口的,否则根本就不能用. 下面是我之前学习时候写的 ...

  10. 第二十篇:在SOUI中使用分层窗口

    从Windows 2K开始,MS为UI开发引入了分层窗口这一窗口风格.使用分层窗口,应用程序的主窗口可以是半透明,也可以是逐点半透明(即每一个像素点的透明度可以不同). 可以说,正是因为有了分层窗口, ...