/** * jquery plugin to obtain touch gestures from iphone, ipod touch and ipad, should also work with android mobile phones (not tested yet!) * common usage: wipe images (left and right to show the previous or next image) * * @author andreas waltl, netcu internetagentur (http://www.netcu.de) * @version 1.1.1 (9th december 2010) - fix bug (older ie's had problems) * @version 1.1 (1st september 2010) - support wipe up and wipe down * @version 1.0 (15th july 2010) */ (function($) { $.fn.touchwipe = function(settings) { var config = { min_move_x: 20, min_move_y: 20, wipeleft: function() { }, wiperight: function() { }, wipeup: function() { }, wipedown: function() { }, preventdefaultevents: true }; if (settings) $.extend(config, settings); this.each(function() { var startx; var starty; var ismoving = false; function canceltouch() { this.removeeventlistener('touchmove', ontouchmove); startx = null; ismoving = false; } function ontouchmove(e) { var x = e.touches[0].pagex; var y = e.touches[0].pagey; var dx = startx - x; var dy = starty - y; if(config.preventdefaultevents) { // prevent horizontal swiping only if(e.touches[0].pagex !== startx && math.abs(dy) <= 5){ e.preventdefault(); } } if(ismoving) { if(math.abs(dx) >= config.min_move_x) { canceltouch(); if(dx > 0) { config.wipeleft(); } else { config.wiperight(); } } else if(math.abs(dy) >= config.min_move_y) { canceltouch(); if(dy > 0) { config.wipedown(); } else { config.wipeup(); } } } } function ontouchstart(e) { if (e.touches.length == 1) { startx = e.touches[0].pagex; starty = e.touches[0].pagey; ismoving = true; this.addeventlistener('touchmove', ontouchmove, false); } } if ('ontouchstart' in document.documentelement) { this.addeventlistener('touchstart', ontouchstart, false); } }); return this; }; })(jquery);