javascript - Jquery draggable/droppable and sortable combined -
i asked create grid of squares, each square may or may not contain link , these links should able moved grid.
i figured draggable/droppable way go , works fine, however, want able have draggables swap if 1 dropped on top of another. looks sortable have been way go. however, sortable looks more it's meant lists whereas haven't written list.
is there easy way make have far work, or need totally rewrite using sortable? i'm new javascript, took me while far, , i'm not asking code dread thought of having figure whole thing out again! opinions?
here's fiddle of code: http://jsfiddle.net/kvvkt/
and code:
html
<div id="gridcontainer"> <div id="-2:-2" class="droppable occupied"> <a href="" id ="508" class="bookmark draggable white" title="white" "target="_blank">1</a> </div> <div id="-2:-1" class="droppable">2</div> <div style="clear:both"></div> <div id="-2:0" class="droppable">3</div> <div id="-2:1" class="droppable occupied"> <a href="" id ="567" class="bookmark draggable white" title="white" "target="_blank">4</a> </div> <div style="clear:both"></div> </div>
css
#gridcontainer{position:relative;padding:25px;color:#ff0004;background:#ffffff;} .droppable{width:65px; height:65px;float:left; margin:5px;background:#000000;} .bookmark {float:left;width:65px; height:65px;display:block;position:absolute;} .position{float:left;width:65px; height:65px;display:block;} .position:hover{background-image:url(../img/tilehover.png);} .bookmark.ui-draggable-dragging {-moz-box-shadow: 0 0 5px #d9d9d9; -webkit-box-shadow: 0 0 5px #d9d9d9; box-shadow: 0 0 5px #d9d9d9;} .draggable{ background:#888888;} [draggable] { -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; user-select: none; }
javascript
$('.draggable').draggable({ start: function() {$('#dropdownaddtile').slidedown();}, stop: function() {$('#dropdownaddtile').slideup();}, containment: '#container', snap:'.droppable', snapmode:'inner', revert:'invalid',snaptolerance: 32}); $('.droppable').droppable({drop: handledropevent, accept: function(e){if(e.hasclass('draggable')) { if (!$(this).hasclass('occupied')) {return true; }}}}); function handledropevent( event, ui ) { event.preventdefault(); var draggable = ui.draggable; var droppable = $(this); var droppableid = $(this).attr("id"); var draggableid = ui.draggable.attr("id"); $('.draggable').draggable({containment: '#container', snap:'.droppable', snapmode:'inner', revert:'invalid',snaptolerance: 32}); $('.droppable').droppable({drop: handledropevent, accept: function(e){if(e.hasclass('draggable')) { if (!$(this).hasclass('occupied')) {return true; }}}}); var initposit = ui.draggable.parent().attr('id'); //save new position $.post("tiles/updatetileposition", { draggableid:draggableid, droppableid: droppableid }).done(function(data) { //alert(data); }) $(this).addclass('occupied'); ui.draggable.parent().removeclass('occupied'); if($(ui.draggable).parent() !==$(this)){ $(ui.draggable).appendto($( )); } }
any opinions gratefully received
first, never use ids begin number or symbol. according solution:
id , name tokens must begin letter ([a-za-z]) , may followed number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), , periods (".").
now let's clean of code. current situation, recommend using .draggable()
, .droppable()
.
i started scratch, kept draggable
& droppable
options used in jsfiddle. here came (feel free change aspect of it):
demo: http://jsfiddle.net/dirtyd77/y8dlz/
http://fiddle.jshell.net/dirtyd77/y8dlz/show/
javascript:
$(function () { $('#container div').draggable({ containment: "#container", helper: 'clone', snap: true, snapmode: 'inner', snaptolerance: 32, revert: 'invalid' }); $('#container div').droppable({ hoverclass: 'ui-state-highlight', drop: function (event, ui) { var _drop = $(this), _drag = $(ui.draggable), _dropchildren = _drop.children(), //original drop children _dragchilden = _drag.children(); //original drag children if(_dropchildren.length > 0){ _dropchildren.appendto(_drag); } _dragchilden.appendto(_drop); } }); });
html:
<div id="container"> <div> <a href="#somelink1" class="link1">link 1</a> </div> <div></div> <div> <a href="#somelink2" class="link2">link 2</a> </div> <div></div> <div> <a href="#somelink3" class="link3">link 3</a> </div> <div> <a href="#somelink1" class="link1">link 4</a> <a href="#somelink3" class="link3">link 5</a> </div> </div>
css:
div:not(#container) { border:1px solid orange; } #container { padding:20px; margin:10px; float: left; /* needed containment option */ width: 336px; height: auto; border:1px solid green; } #container div { margin: 4px; padding: 0px; float: left; width: 100px; height: 90px; text-align: center; } .link1{ color: purple; } .link2{ color: red; } .link3{ color: green; }
i hope you're looking for! please let me know if need further explanation or have additional questions! happy coding!
Comments
Post a Comment