Last active
August 29, 2015 14:10
-
-
Save ui2code/c4f172ba1aa07bd255c4 to your computer and use it in GitHub Desktop.
Drag drop js puro
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
| // HTML | |
| <div id="bin"></div> | |
| <ul> | |
| <li><a id="one" href="#">one</a></li> | |
| <li><a href="#" id="two">two</a></li> | |
| <li><a href="#" id="three">three</a></li> | |
| <li><a href="#" id="four">four</a></li> | |
| <li><a href="#" id="five">five</a></li> | |
| </ul> | |
| // CSS | |
| body { font-family: helvetica, arial; } | |
| li { list-style: none; } | |
| li a { | |
| text-decoration: none; | |
| color: #000; | |
| margin: 10px; | |
| width: 150px; | |
| border: 3px dashed #999; | |
| background: #eee; | |
| padding: 10px; | |
| display: block; | |
| } | |
| *[draggable=true] { | |
| -moz-user-select:none; | |
| -khtml-user-drag: element; | |
| cursor: move; | |
| } | |
| *:-khtml-drag { | |
| background-color: rgba(238,238,238, 0.5); | |
| } | |
| a:hover:after { | |
| content: ' (drag me)'; | |
| } | |
| li.over { | |
| border-color: #333; | |
| background: #ccc; | |
| } | |
| #bin { | |
| background: url(http://placehold.it/200x200/cccccc) top right no-repeat; | |
| height: 250px; | |
| width: 166px; | |
| float: right; | |
| border: 5px solid #000; | |
| position: relative; | |
| } | |
| #bin.over { | |
| background: url(http://placehold.it/200x200/ffcc00) top left no-repeat; | |
| } | |
| #bin p { | |
| font-weight: bold; | |
| text-align: center; | |
| position: absolute; | |
| bottom: 20px; | |
| width: 166px; | |
| font-size: 32px; | |
| color: #fff; | |
| text-shadow: #000 2px 2px 2px; | |
| } | |
| // Javascript | |
| var eat = ['item1', 'item2', 'item3', 'item4']; | |
| var yum = document.createElement('p'); | |
| var msie = /*@cc_on!@*/0; | |
| yum.style.opacity = 1; | |
| var links = document.querySelectorAll('li > a'), | |
| el = null; | |
| for (var i = 0; i < links.length; i++) { | |
| el = links[i]; | |
| el.setAttribute('draggable', 'true'); | |
| addEvent(el, 'dragstart', function (e) { | |
| e.dataTransfer.effectAllowed = 'move'; | |
| e.dataTransfer.setData('Text', this.id); // obrigatório, senão não funciona | |
| }); | |
| } | |
| var bin = document.querySelector('#bin'); | |
| function addEvent(elem, event, func ) { | |
| if (!!window.attachEvent) elem.attachEvent('on' + event, func); | |
| else elem.addEventListener(event, func, false); | |
| } | |
| addEvent(bin, 'dragover', function (e) { | |
| if (e.preventDefault) e.preventDefault(); // permite o drop | |
| this.className = 'over'; | |
| return false; | |
| }); | |
| // para trabalhar com o IE | |
| addEvent(bin, 'dragenter', function (e) { | |
| this.className = 'over'; | |
| return false; | |
| }); | |
| addEvent(bin, 'dragleave', function () { | |
| this.className = ''; | |
| }); | |
| addEvent(bin, 'drop', function (e) { | |
| if (e.stopPropagation) e.stopPropagation(); | |
| var el = document.getElementById(e.dataTransfer.getData('Text')); | |
| el.parentNode.removeChild(el); | |
| // texto + efeito de fade | |
| bin.className = ''; | |
| yum.innerHTML = eat[parseInt(Math.random() * eat.length)]; | |
| var y = yum.cloneNode(true); | |
| bin.appendChild(y); | |
| setTimeout(function () { | |
| var t = setInterval(function () { | |
| if (y.style.opacity <= 0) { | |
| if (msie) { // don't bother with the animation | |
| y.style.display = 'none'; | |
| } | |
| clearInterval(t); | |
| } else { | |
| y.style.opacity -= 0.1; | |
| } | |
| }, 50); | |
| }, 250); | |
| return false; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment