sortable.js onUpdate 拖拽元素更改变化的事件,元素的增加减少并不会触发,而onSort无论是位置变化还是元素的增加移除都会执行onSort事件。
属性名称 | 类型 | 说明 |
---|---|---|
onUpdate | function | 拖拽元素更改变化的事件,元素的增加减少并不会触发 |
//选中回调函数,evt为参数,要查看evt对象属性请在谷歌浏览器按F12,然后再控制台(console选项卡)查看
//获取对象
var el = document.getElementById('itxst');
//设置配置
var ops = {
animation: 1000,
draggable: ".item",
direction: 'vertical',
forceFallback:true,
//*************** 位置改变后的回调事件 ***************
onUpdate: function (evt) {
console.log(evt);
var index=evt.oldIndex;
var arr = sortable.toArray();
document.getElementById("msg").innerHTML = "新的顺序是:" + JSON.stringify(arr);
}
};
//初始化
var sortable = Sortable.create(el, ops);
代码例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>sortable.js onUpdate事件例子</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>位置调整后会触发onUpdate事件</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,
//*************** 位置改变后的回调事件 ***************
onUpdate: function (evt) {
console.log(evt);
var index = evt.oldIndex;
var arr = sortable.toArray();
document.getElementById("msg").innerHTML = "新的顺序是:" + JSON.stringify(arr);
}
};
//初始化
var sortable = Sortable.create(el, ops);
</script>
</body>
</html>