Created
August 17, 2012 18:15
-
-
Save bhurlow/3381259 to your computer and use it in GitHub Desktop.
Backbone Drag and Drop (quick and dirty)
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
DragDrop = Backbone.View.extend { | |
render: () -> | |
thisView = this | |
dragArea = @options.drag | |
dropArea = @options.drop | |
dropAreaY = $(dropArea).offset().top | |
dropAreaX = $(dropArea).offset().left | |
$dragAreaChildren = $(dragArea).children() | |
$dragAreaChildren.on 'touchstart', (e) -> | |
e.preventDefault() | |
movingChild = $(e.target) | |
movingChild.on 'touchmove', (e) -> | |
e.preventDefault() | |
if(e.touches.length == 1) | |
touch = e.touches[0] | |
node = touch.target | |
node.style.position = "absolute" | |
node.style.color = "red" | |
node.style.left = touch.pageX + "px" | |
node.style.top = touch.pageY + "px" | |
movingChild.on 'touchend', (e) -> | |
releasePositionX = e.changedTouches[0].pageX | |
releasePositionY = e.changedTouches[0].pageY | |
if releasePositionX >= dropAreaX and releasePositionY >= dropAreaY | |
childText = $(this).text() | |
$(dropArea).append '<h1>' + childText + '</h1>' | |
$(this).hide() | |
} |
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
<div id='dragArea'> | |
<h1> Item 1 </h1> | |
<h1> Item 2 </h2> | |
<h1> Item 3 </h3> | |
</div> | |
<div id='dropArea'></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment