gpt4 book ai didi

javascript - Jquery 可拖动/可放置和可排序组合

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:42 25 4
gpt4 key购买 nike

我被要求创建一个正方形网格,其中每个正方形可能包含也可能不包含链接,并且这些链接应该能够在网格中移动。

我认为 draggable/droppable 将是可行的方法并且它工作正常,但是,现在我们希望能够让 draggables 在一个拖放到另一个之上时交换。所以现在看起来 sortable 是可行的方法。但是,sortable 看起来更像是用于列表,而我并没有把它写成列表。

是否有一种简单的方法可以使我目前的工作正常进行,或者我是否需要使用 sortable 完全重写它?我是 javascript 的新手,我花了一段时间才走到这一步,我不是要代码,但我害怕不得不再次弄清楚整个事情!有什么意见吗?

这是我的一小段代码:http://jsfiddle.net/kvVkT/

和代码:

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($( this ));
}
}

感谢收到的任何意见

最佳答案

首先,永远不要使用以数字或符号开头的 ID。根据这个solution :

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").


现在让我们清理您的一些代码。对于你目前的情况,我建议使用.draggable().droppable() .

我从头开始,但保留了 jsfiddle 中使用的 draggabledroppable 选项.这是我想出的(随意更改它的任何方面):

演示: 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 for 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;
}

希望这就是您要找的!如果您需要任何进一步的解释或有任何其他问题,请告诉我!编码愉快!

关于javascript - Jquery 可拖动/可放置和可排序组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16118853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com