Created
June 16, 2017 15:09
-
-
Save rigred/6da812bc729c0ef74a81ab9f8594b856 to your computer and use it in GitHub Desktop.
old touch swipe jquery plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Usage: | |
$("#element").touchwipe({ | |
wipeLeft: function() { alert("left"); }, | |
wipeRight: function() { alert("right"); }, | |
wipeUp: function() { alert("up"); }, | |
wipeDown: function() { alert("down"); }, | |
min_move_x: 20, | |
min_move_y: 20, | |
preventDefaultEvents: true | |
}); | |
*/ | |
(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) { | |
if(config.preventDefaultEvents) { | |
e.preventDefault(); | |
} | |
if(isMoving) { | |
var x = e.touches[0].pageX; | |
var y = e.touches[0].pageY; | |
var dx = startX - x; | |
var dy = startY - y; | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment