Get the drag distance
Description
The following code shows how to get the drag distance.
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet"
href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script
src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head><!--from www. jav a 2 s.co m-->
<body>
<div data-role="page">
<div data-role="content">
<p>Some content, drag to see the distance</p>
</div>
</div>
<script type='text/javascript'>//<![CDATA[
var coords = [0, 0],
threshold = 100;//in pixels
$(document).bind('vmousedown', function (event) {
coords = [event.offsetX, event.offsetY];
}).bind('vmouseup', function () {
var distance = Math.sqrt(Math.pow(coords[0] - event.offsetX, 2) + Math.pow(coords[1] - event.offsetY, 2));
if (distance > threshold) {
alert('Swipe (' + Math.floor(distance) + 'px)');
} else {
alert('Click (' + Math.floor(distance) + 'px)');
}
});
</script>
</body>
</html>