Last active
December 31, 2015 05:19
-
-
Save ksheedlo/7940210 to your computer and use it in GitHub Desktop.
HTML5 Drag n Drop
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
{ | |
"name": "dnd-demo", | |
"version": "0.0.1", | |
"authors": [ | |
"Ken Sheedlo <[email protected]>" | |
], | |
"description": "HTML5 drag-n-drop table prototype", | |
"main": "index.js", | |
"license": "MIT", | |
"private": true, | |
"ignore": [ | |
"**/.*", | |
"node_modules", | |
"bower_components", | |
"test", | |
"tests" | |
], | |
"devDependencies": { | |
"bootstrap": "~3.0.3" | |
} | |
} |
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
[draggable] { | |
-moz-user-select: none; | |
-khtml-user-select: none; | |
-webkit-user-select: none; | |
user-select: none; | |
-khtml-user-drag: element; | |
-webkit-user-drag: element; | |
} | |
.ks-dnd-table { | |
max-width: 100%; | |
margin-bottom: 20px; | |
} | |
.ks-dnd-table .row { | |
padding: 8px; | |
line-height: 1.428571429; | |
vertical-align: top; | |
border-top: 1px solid #ddd; | |
list-style: none; | |
} | |
.ks-dnd-table .row:first-child { | |
border-top: 0; | |
font-weight: bold; | |
} | |
.ks-dnd-table .row:nth-child(2) { | |
border-top: 2px solid #ddd; | |
} | |
.ks-dnd-table .over { | |
border-top: 2px solid red !important; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>HTML5 Drag n Drop Demo</title> | |
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="/demo.css"> | |
</head> | |
<body> | |
<div class="navbar navbar-inverse navbar-static-top"> | |
<div class="container"> | |
<div class="navbar-header"> | |
<a class="navbar-brand" href="/">DND</a> | |
</div> | |
</div> | |
</div> | |
<div class="container"> | |
<ul class="ks-dnd-table"> | |
<li class="row"> | |
<div class="col-sm-2">#</div> | |
<div class="col-sm-3">First Name</div> | |
<div class="col-sm-3">Last Name</div> | |
<div class="col-sm-3">Username</div> | |
</li> | |
<li class="row" draggable="true"> | |
<div class="col-sm-2">1</div> | |
<div class="col-sm-3">Kenny</div> | |
<div class="col-sm-3">Sheedlo</div> | |
<div class="col-sm-3">@kensheedlo</div> | |
</li> | |
<li class="row" draggable="true"> | |
<div class="col-sm-2">2</div> | |
<div class="col-sm-3">Yolo</div> | |
<div class="col-sm-3">Swaggins</div> | |
<div class="col-sm-3">@yolo</div> | |
</li> | |
<li class="row" draggable="true"> | |
<div class="col-sm-2">3</div> | |
<div class="col-sm-3">Marsellus</div> | |
<div class="col-sm-3">Wallace</div> | |
<div class="col-sm-3">@laprivileges</div> | |
</li> | |
<li class="row"></li> | |
</ul> | |
</div> | |
<script type="text/javascript" src="/index.js"></script> | |
</body> | |
</html> |
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
/** | |
* Ken Sheedlo | |
* HTML5 Drag n Drop Demo without jQuery or Angular | |
* | |
* install bootstrap and run this with a python server: | |
* | |
* $ bower install | |
* $ python -m SimpleHTTPServer | |
* | |
*/ | |
(function (window, document) { | |
'use strict'; | |
var rows, | |
dragSrcElt = null, | |
dragTarget = null, | |
handleDragStart, | |
handleDragEnter, | |
handleDragEnd; | |
handleDragStart = function (e) { | |
this.classList.add('dragging'); | |
dragTarget = dragSrcElt = this; | |
e.dataTransfer.effectAllowed = 'move'; | |
e.dataTransfer.setData('text/html', this.innerHTML); | |
}; | |
handleDragEnter = function () { | |
rows.forEach(function (row) { | |
row.classList.remove('over'); | |
}); | |
this.classList.add('over'); | |
dragTarget = this; | |
}; | |
handleDragEnd = function () { | |
var parentElement; | |
this.classList.remove('dragging'); | |
rows.forEach(function (row) { | |
row.classList.remove('over'); | |
}); | |
if (dragSrcElt !== dragTarget) { | |
parentElement = dragSrcElt.parentElement; | |
parentElement.insertBefore(dragSrcElt, dragTarget); | |
} | |
}; | |
rows = Array.apply(null, document.querySelectorAll('.ks-dnd-table .row')).slice(1); | |
rows.slice(0, -1).forEach(function (row) { | |
row.addEventListener('dragstart', handleDragStart.bind(row), false); | |
row.addEventListener('dragenter', handleDragEnter.bind(row), false); | |
row.addEventListener('dragend', handleDragEnd.bind(row), false); | |
}); | |
rows.slice(-1).forEach(function (row) { | |
row.addEventListener('dragenter', handleDragEnter.bind(row), false); | |
}); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment