sortable.js onClone 克隆事件

sortable.js onClone 克隆事件,group属性设置了clone后多组间拖拽时会触发该事件。

onClone事件

属性名称 类型 说明
onClone function 克隆时会触发该事件
//获取对象
var el = document.getElementById('g1');
//设置配置
var ops1 = {
    group: { name: "itxst.com", pull: 'clone', put: false },
    animation: 1000,
    draggable: ".item",
    direction: 'vertical',
    forceFallback: true,
    //*********  克隆事件需要设置 请注意group属性的pull设置 *********
    onClone: function (evt) {
        //debugger;
        //被克隆的对象
        var origEl = evt.item;
        //克隆后的对象
        var cloneEl = evt.clone;
        cloneEl.innerHTML = "新的" + cloneEl.dataset.id;
    } 
};
//初始化
var sortable1 = Sortable.create(el, ops1);

代码例子

evt返回参数对象结构

完整代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>sortable.js onClone事件例子</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, minimal-ui">
    <script src="http://www.itxst.com/package/sortable/sortable.min.js"></script>
    <style>
        .itxst {
            margin: 10px;
            width: 40%;
            float: left;
        }
       .itxst  div {
                padding: 6px;
                background-color: #fdfdfd;
                border: solid 1px #eee;
                margin-bottom: 10px;
                cursor: move;
            } 
        .msg {
            clear: both;
        }
    </style>
</head>
<body>
    <span>把a组元素往b组拖拽试试看</span>
    <div>
        <div id="g1" class="itxst">
            <div class="item" data-id="a1">item a1</div>
            <div class="item" data-id="a2">item a2</div>
            <div class="item" data-id="a3">item a3</div>
        </div>
        <div id="g2" class="itxst">
            <div class="item" data-id="b1">item b1</div>
            <div class="item" data-id="b2">item b2</div>
            <div class="item" data-id="b3">item b3</div>
        </div>
    </div>
    <div id="msg1" class="msg"></div>
    <div id="msg2" class="msg"></div>
    <script>
        //获取对象
        var el = document.getElementById('g1');
        //设置配置
        var ops1 = {
            group: { name: "itxst.com", pull: 'clone', put: false },
            animation: 1000,
            draggable: ".item",
            direction: 'vertical',
            forceFallback: true,
            //*********  克隆事件需要设置 请注意group属性的pull设置 *********
            onClone: function (evt) {
                //debugger;
                //被克隆的对象
                var origEl = evt.item;
                //克隆后的对象
                var cloneEl = evt.clone;
                cloneEl.innerHTML = "新的" + cloneEl.dataset.id;
            }
        };
        //初始化
        var sortable1 = Sortable.create(el, ops1);


        //获取对象
        var e2 = document.getElementById('g2');
        //设置配置
        var ops2 = {
            group: { name: "itxst.com", pull: 'clone', put: true },
            animation: 1000,
            draggable: ".item",
            direction: 'vertical',
            forceFallback: true,
            //*********  克隆事件需要设置 *********
            onClone: function (evt) {
                //debugger;
                //被克隆的对象
                var origEl = evt.item;
                //克隆后的对象
                var cloneEl = evt.clone;
                cloneEl.innerHTML = "新的" + cloneEl.dataset.id;
            }
        };
        //初始化
        var sortable2 = Sortable.create(e2, ops2);
    </script>
</body>
</html>