jQuery Multi-TouchWipe / Multi-TouchZoom
jQuery Multi-TouchWipe / Multi-TouchZoom是小弟参照WipeTouch扩展出来的针对多点触屏划动而改写出来的Jquery插件,可以为dom上的两手指触屏划动拨入(zoomin)或者拨开(zoomout)操作而绑定自己的事件。
// jQuery multitouchzoom 1.0.0
// ------------------------------------------------------------------------
//
// Developed and maintained by Nelson
// Inspired by and referenced from Igor Ramadas's WipeTouch. //
// USAGE
// ------------------------------------------------------------------------
//
// $(selector).multitouchzoom(config);
//
// The multitouchzoom events should expect 2 fingers(a finger and b finger) touch on the screen at the same time;
//
//
// EXAMPLE
// $(document).multitouchzoom({
// zoomIn: function(result) { alert("zoomIn "); },
// zoomOut: function(result) { alert("zoomOut "); }
// });
//
//
// More details at https://github.com/nelsonkuang/MultiTouchZoom
//
//
// The minified version of WipeTouch can be generated using Jasc: http:// (function ($) {
$.fn.multitouchzoom = 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
preventDefault: true, // if true, prevents default events (click for example) // Multi Touch Zoom events
zoomIn: false, // called on zoom in gesture
zoomOut: false, // called on zoom out gesture
multiTouchMove: false, // triggered whenever touchMove acts }; if (settings) {
$.extend(config, settings);
} this.each(function () {
// ------------------------------------------------------------------------
// INTERNAL VARIABLES
// ------------------------------------------------------------------------
var startDate = false; // used to calculate timing and aprox. acceleration
var touchedElement = false; // element which user has touched
var clickEvent = false; // holds the click event of the target, when used hasn't clicked var aStartX; // where finger "a" touch has started, left
var aStartY; // where finger "a" touch has started, top
var aCurX; // keeps finger "a" touch X position while moving on the screen
var aCurY; // keeps finger "a" touch Y position while moving on the screen
var aIsMoving = false; // is user's finger "a" touching and moving? var bStartX; // where finger "b" touch has started, left
var bStartY; // where finger "b" touch has started, top
var bCurX; // keeps finger "b" touch X position while moving on the screen
var bCurY; // keeps finger "b" touch Y position while moving on the screen
var bIsMoving = false; // is user's finger "b" touching and moving? // ------------------------------------------------------------------------
// Multi Touch Eevents
// ------------------------------------------------------------------------ // Called when user multi-touches the screen.
function onMultiTouchStart(e) {
var aStart = e.originalEvent.touches[0] && e.originalEvent.touches.length > 1;
var bStart = e.originalEvent.touches[1];
if (!aIsMoving && !bIsMoving && aStart && bStart) {
if (config.preventDefault) {
e.preventDefault();
} aStartX = e.originalEvent.touches[0].pageX;
aStartY = e.originalEvent.touches[0].pageY;
bStartX = e.originalEvent.touches[1].pageX;
bStartY = e.originalEvent.touches[1].pageY; $(this).bind("touchmove", onMultiTouchMove); // Set the start date and current X/Y for finger "a" & finger "b".
startDate = new Date().getTime();
aCurX = aStartX;
aCurY = aStartY;
bCurX = bStartX;
bCurY = bStartY;
aIsMoving = true;
bIsMoving = 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.
$(this).unbind("touchmove", onMultiTouchMove); // If is moving then calculate the touch results, otherwise reset it.
if (aIsMoving || bIsMoving) {
touchCalculate(e);
}
else {
resetTouch();
}
} // Called when user is touching and moving on the screen.
function onMultiTouchMove(e) {
if (config.preventDefault) {
e.preventDefault();
} if (aIsMoving || bIsMoving) {
aCurX = e.originalEvent.touches[0].pageX;
aCurY = e.originalEvent.touches[0].pageY;
bCurX = e.originalEvent.touches[1].pageX;
bCurY = e.originalEvent.touches[1].pageY;
// If there's a MultiTouchMove event, call it passing
// current X and Y position (curX and curY).
if (config.multiTouchMove) {
triggerEvent(config.multiTouchMove, {
aCurX: aCurX,
aCurY: aCurY,
bCurX: bCurX,
bCurY: bCurY
});
}
}
} // ------------------------------------------------------------------------
// 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 ax = aCurX; // current left position of finger 'a'
var ay = aCurY; // current top position of finger 'a'
var bx = bCurX; // current left position of finger 'b'
var by = bCurY; // current top position of finger 'b'
var dax = ax - aStartX; // diff of current left to starting left of finger 'a'
var day = ay - aStartY; // diff of current top to starting top of finger 'a'
var dbx = bx - bStartX; // diff of current left to starting left of finger 'b'
var dby = by - bStartY; // diff of current top to starting top of finger 'b'
var aax = Math.abs(dax); // amount of horizontal movement of finger 'a'
var aay = Math.abs(day); // amount of vertical movement of finger 'a'
var abx = Math.abs(dbx); // amount of horizontal movement of finger 'b'
var aby = Math.abs(dby); // amount of vertical movement of finger 'b' //diff of current distance to starting distance between the 2 points
var diff = Math.sqrt((aStartX - bStartX) * (aStartX - bStartX) + (aStartY - bStartY) * (aStartY - bStartY)) - Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)); // If moved less than 15 pixels, touch duration is less than 100ms,
// then trigger a click event and stop processing.
if (aax < 15 && aay < 15 && abx < 15 && aby < 15 && ms < 100) {
clickEvent = false; if (config.preventDefault) {
resetTouch(); touchedElement.trigger("click");
return;
}
} // Is it zooming in or out?
var isZoomIn = diff > 0;
var isZoomOut = diff < 0; // Calculate speed from 1 to 5, 1 being slower and 5 faster.
var as = ((aax + aay) * 60) / ((ms) / 6 * (ms));
var bs = ((abx + aby) * 60) / ((ms) / 6 * (ms)); if (as < 1) as = 1;
if (as > 5) as = 5; if (bs < 1) bs = 1;
if (bs > 5) bs = 5; var result = {
aSpeed: parseInt(as),
bSpeed: parseInt(bs),
aX: aax,
aY: aay,
bX: abx,
bY: aby,
source: touchedElement
}; if (aax >= config.moveX || abx >= config.moveX||aay>= config.moveY||aby>=config.moveY) {
// If it is zooming in, trigger zoomIn events.
if (isZoomIn) {
triggerEvent(config.zoomIn, result);
}
// Otherwise trigger zoomOut events.
else if (isZoomOut) {
triggerEvent(config.zoomOut, result);
}
}
resetTouch();
} // Resets the cached variables.
function resetTouch() {
aStartX = false;
aStartY = false;
bStartX = false;
bStartY = false;
startDate = false;
aIsMoving = false;
bIsMoving = 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 event passing a result object with
// aSpeed & bSpeed from 1 to 5, aX / aY & bX / bY movement amount in pixels,
// and the source element.
function triggerEvent(zoomEvent, result) {
if (zoomEvent) {
zoomEvent(result);
}
} // ------------------------------------------------------------------------
// ADD MULTITOUCHSTART AND TOUCHEND EVENT LISTENERS
// ------------------------------------------------------------------------ if ("ontouchstart" in document.documentElement) { $(this).bind("touchstart", onMultiTouchStart);
$(this).bind("touchend", onTouchEnd);
}
}); return this;
};
})(jQuery);
jQuery Multi-TouchWipe / Multi-TouchZoom的更多相关文章
- How to set an Apache Kafka multi node – multi broker cluster【z】
Set a multi node Apache ZooKeeper cluster On every node of the cluster add the following lines to th ...
- python multi process multi thread
muti thread: python threading: https://docs.python.org/2/library/threading.html#thread-objects https ...
- am335x using brctl iptables dhcpcd make multi wan & multi lan network(十五)
构建多LAN口多WAN口动态网络 [目的] 在AM335X定制动态网络功能,如下所示,在系统当中有两个以太网口,有4G模块,有wifi芯片8188eu支持AP+STA功能. [实验环境] 1. Ub ...
- redis multi exec
multi(),返回一个redis对象,并进入multi-mode模式,一旦进入multi-mode模式,以后调用的所有方法都会返回相同的对象,直到exec()方法被调用. phpredis是php的 ...
- [译]The multi Interface
The multi Interfacemulti接口 The easy interface as described in detail in this document is a synchrono ...
- 科学 multi port
issues/679 create new UUID cat /proc/sys/kernel/random/uuid example config : multi port , multi user ...
- asp.net mvc 2.o 中使用JQuery.uploadify
From:http://www.cnblogs.com/strugglesMen/archive/2011/07/01/2095916.html 官方网站http://www.uploadify.co ...
- jquery uploadify修改上传的文件名和显示
如果觉得看文章太麻烦,可以直接看参考:http://stackoverflow.com/questions/7707687/jquery-uploadify-change-file-name-as-i ...
- widget jquery 理解
jquery ui 的所有组件都是基于一个简单,可重用的widget. 这个widget是jquery ui的核心部分,实用它能实现一致的API,创建有状态的插件,而无需关心插件的内部转换. $.wi ...
- jquery ui widget 源代码分析
jquery ui 的全部组件都是基于一个简单,可重用的widget. 这个widget是jquery ui的核心部分,有用它能实现一致的API.创建有状态的插件,而无需关心插件的内部转换. $.wi ...
随机推荐
- .NET微信公众号开发-1.0初始微信公众号
一.前言 微信公众号是开发者或商家在微信公众平台上申请的应用账号,该帐号与QQ账号互通,通过公众号,商家可在微信平台上实现和特定群体的文字.图片.语音.视频的全方位沟通.互动 .形成了一 种主流的线上 ...
- Hibernate双向一对一对象关系模型映射
一个员工一辆车:one-to-one 实现一:让汽车表中的外键唯一 create table emp ( eid int primary key auto_increment, ename varch ...
- 解决Idea创建maven-archetype-webapp项目无java目录的问题
一.背景 在适用IDEA创建maven-archetype-webapp项目的时候,创建完成后发现在main文件夹下没有java源文件夹,不少小伙伴也遇到该问题,但不知道怎么解决,下面我就来分享解决步 ...
- 忘记Mysql登录密码
1,使用安全模式跳过验证: 如果 Mysql在运行,Kill掉. 如果mysqld_safe无法启动,可用管理员权限sudo . 2,本地登录: 启动Mysql 3,修改密码: 5.7之后, 更改密码 ...
- hdu 1249 三角形
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 part=3*s*(s-1)+2 #include<stdio.h> #includ ...
- nfs 是Network File System 网络文件系统
NFS的基本原刚是容许不同的客户端及服务通过一组PRC分享相同的文件系统,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享.NFS在文件传送过程中依赖于RPC协议.远程过程调用Rem ...
- C#的正则表达式
using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...
- HTML5学习之文件操作(九)
之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很进行跨平台的处理,另外就是让我们的web应用依赖了第三方的插件,而不是很独立 ...
- 【翻译十三】java-并发之饥饿与活锁
Starvation and Livelock Starvation and livelock are much less common a problem than deadlock, but ar ...
- SSH入门简单搭建例子
因为公司涉及项目使用SSH,为了解SSH搭建方式和运作原理,就自己搭建了一个. 采用尽量以最少的JAR包,搭建一个简单的struts2+spring+hibernate环境,希望像我这样的入门者都能理 ...