sortable.js onUnchoose 取消选中事件

sortable.js 选中需要拖拽对象后松开鼠标会执行onUnchoose事件,表示用户放弃了拖拽行为。

onUnchoose事件

属性名称类型说明
onUnchoosefunction指定放弃选中对象执行的回调方法
//选中回调函数,evt为参数,要查看evt对象属性请在谷歌浏览器按F12,然后再控制台(console选项卡)查看
 onUnchoose: function (evt) {  
       console.log(evt);
       var index=evt.oldIndex;  
       document.getElementById("msg").innerHTML ="取消选中的第"+index+"个元素"
 }

代码例子

evt返回参数对象结构

完整代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>sortable.js onUnchoose事件例子</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="https://www.itxst.com/package/sortable/sortable.min.js"></script>
    <style>
        #itxst {
            margin: 10px;
            width: 80%; 
        }
        #itxst div {
                padding: 6px;
                background-color: #fdfdfd;
                border: solid 1px #eee;
                margin-bottom: 10px;
                cursor: move;  
            }
    </style>
</head>
<body>
  <span>鼠标点击下面的元素然后松开试试看</span>
    <div id="itxst">
        <div class="item" data-id="1">item 1</div>
        <div class="item" data-id="2">item 2</div>
        <div class="item" data-id="3">item 3</div>
    </div>
    <div id="msg"></div>
    <script>
        //获取对象
        var el = document.getElementById('itxst');
        //设置配置
        var ops = {
            animation: 1000,
            draggable: ".item",
            direction: 'vertical', 
            forceFallback:true,
            //*************** 取消选中的事件 ***************
             onUnchoose: function (evt) {  
                   console.log(evt);
                   var index=evt.oldIndex;  
                   document.getElementById("msg").innerHTML ="取消选中的第"+index+"个元素"
              },
          //选中回调函数
           onChoose: function (evt) {  
                 console.log(evt);
                 var index=evt.oldIndex;  
                 document.getElementById("msg").innerHTML ="你选中了第"+index+"个元素"
            },
            //拖动结束
            onEnd: function (evt) {
                console.log(evt);
                //获取拖动后的排序
                var arr = sortable.toArray();
                
               document.getElementById("msg").innerHTML = "A组排序结果:" + JSON.stringify(arr);
            },
        };
        //初始化
        var sortable = Sortable.create(el, ops);
    </script>
</body>
</html>