Created
June 5, 2014 22:04
-
-
Save jordangarcia/30d73b5bf8c4811e9893 to your computer and use it in GitHub Desktop.
Example of drag and drop using Hammer.js
This file contains hidden or 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
.box { | |
padding: 5px; | |
background: red; | |
margin: 10px; | |
position:relative; | |
z-index: 3; | |
} | |
.drag-target { | |
background: blue; | |
z-index: 1; | |
} | |
.drag-target-box { | |
position: absolute; | |
width: 100%; | |
height: 100%; | |
top: 0; | |
left: 0; | |
z-index: 2; | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script> | |
<script src="http://rawgit.com/EightMedia/hammer.js/1.1.3/hammer.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<style> | |
</style> | |
</head> | |
<body> | |
<script> | |
</script> | |
<div draggable="true" class="box"> | |
This text <strong>may</strong> be dragged. | |
</div> | |
<div class="box drag-target">target</div> | |
</body> | |
</html> |
This file contains hidden or 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
Hammer($('.box').get(0)).on('dragstart', function(event) { | |
console.log('dragstart', event); | |
}); | |
Hammer($('.box').get(0)).on('drag', function(event){ | |
// console.log('drag', event.gesture.deltaX, event.gesture.deltaY) | |
var target = event.target; | |
$(target).css({ | |
'transform': 'translate(' + event.gesture.deltaX + 'px,' + event.gesture.deltaY + 'px)' | |
}); | |
}); | |
Hammer(document.body).on('release', function(event){ | |
console.log('release', event); | |
event.gesture.preventDefault() | |
}); | |
Hammer(document.body).on('dragend', function(event) { | |
console.log('dragend', event); | |
$(event.target).css({'transform': 'translate(0,0)'}); | |
debugger; | |
var dropEl = document.elementFromPoint(event.gesture.center.pageX, event.gesture.center.pageY); | |
console.log('dropped on', dropEl); | |
if ($(dropEl).hasClass('drop-target')) { | |
console.log('dropped on drop target'); | |
} | |
}) | |
$(document.body).on('mousedown', '[draggable]', function(event){ | |
console.log('mousedown', event); | |
}) | |
$(document.body).on('mouseup', '[draggable]', function(event){ | |
console.log('mouseup', event); | |
event.preventDefault() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment