sortable.js onChange 拖拽中位置发生改变后的事件,鼠标不用松开也会触发onChange事件。
属性名称 | 类型 | 说明 |
---|---|---|
onChange | function | 拖拽中位置发生改变后的事件 |
//获取对象
var el = document.getElementById('g1');
//设置配置
var ops1 = {
group: { name: "itxst.com", pull: 'clone', put: false },
animation: 1000,
draggable: ".item",
direction: 'vertical',
forceFallback: true,
//********* onChange事件 *********
onChange: function (evt) {
document.getElementById("msg1").innerHTML = "A新的位置:" + evt.newIndex;
}
};
//初始化
var sortable1 = Sortable.create(el, ops1);
代码例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>sortable.js onChange事件例子</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: 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,
//********* onChange事件 *********
onChange: function (evt) {
document.getElementById("msg1").innerHTML = "A新的位置:" + evt.newIndex;
}
};
//初始化
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,
//********* onChange事件 *********
onChange: function (evt) {
document.getElementById("msg2").innerHTML = "B新的位置:" + evt.newIndex;
}
};
//初始化
var sortable2 = Sortable.create(e2, ops2);
</script>
</body>
</html>