sortable.js draggable 指定样式类的元素才允许拖动

This commit is contained in:
bunny 2024-01-31 19:39:42 +08:00
parent 505545aa88
commit 6d928642da
2 changed files with 66 additions and 17 deletions

View File

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>sortable.js draggable属性例子</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>
<div id="itxst">
<div class="item" data-id="1">item 1</div>
<div data-id="2">item 2(此元素未设置class="item" 所以无法拖动)</div>
<div class="item" data-id="3">item 3</div>
</div>
<script>
//获取对象
var el = document.getElementById('itxst');
//设置配置
var ops = {
animation: 1000,
draggable: '.item',
//拖动结束
onEnd: function (evt) {
console.log(evt);
//获取拖动后的排序
var arr = sortable.toArray();
alert(JSON.stringify(arr));
},
};
//初始化
var sortable = Sortable.create(el, ops);
</script>
</body>
</html>

32
temp.js
View File

@ -1,18 +1,16 @@
var g1 = document.getElementById('g1');
var ops1 = {
animation: 166,
//指定可以拖动的元素
draggable: '.item',
//拖动后的数据
dataIdAttr: 'data-no',
//************* 停靠位置样式 **********
ghostClass: 'ghost',
//获取拖动结束的数据
onEnd: function (evt) {
console.log(evt);
//获取拖动后的排序
var arr = sortable1.toArray();
document.getElementById('msg').innerHTML = 'A组排序结果' + JSON.stringify(arr);
},
//获取对象
var el = document.getElementById('itxst');
//设置配置
var ops = {
animation: 1000,
draggable: '.item',
//拖动结束
onEnd: function (evt) {
console.log(evt);
//获取拖动后的排序
var arr = sortable.toArray();
alert(JSON.stringify(arr));
},
};
var sortable1 = Sortable.create(g1, ops1);
//初始化
var sortable = Sortable.create(el, ops);