Jquery draggable/droppable and sortable combined

℡╲_俬逩灬. 提交于 2019-12-05 04:22:24
Dom

First, never use IDs that begin with a number or a symbol. According to this 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 (".").


Now let's clean up some of your code. For your current situation, I recommend using .draggable() and .droppable().

I started from scratch, but kept the draggable & droppable options used in your jsfiddle. Here is what I came up with (feel free to change any 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 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;   
}

I hope this is what you're looking for! Please let me know if you need any further explanation or have any additional questions! Happy coding!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!