sortableJS/15sortable.js onChoose 选中事件...

129 lines
3.6 KiB
HTML
Raw Permalink Normal View History

2024-02-01 09:03:24 +08:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>sortable.js onChoose事件例子</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>
.box {
display: flex;
justify-content: space-around;
width: 50%;
}
.itxst {
margin: 10px auto;
width: 80%;
float: left;
margin-right: 10px;
}
.itxst>div {
padding: 6px;
background-color: #fdfdfd;
border: solid 1px #eee;
margin-bottom: 10px;
cursor: move;
}
.itxst .innner {
padding: 6px;
background-color: #fdfdfd;
border: solid 1px #eee;
margin-bottom: 10px;
cursor: move;
}
#msg {
clear: both;
width: 100%;
}
.ghost {
background-color: red !important;
}
.chosen {
border: solid 2px #3089dc !important;
}
</style>
</head>
<body>
<div class="box">
<div id="g1" class="itxst">
<div class="item" data-id="item1">item 1</div>
<div class="item" data-id="item2">item 2</div>
<div class="item" data-id="item3">item 3</div>
</div>
<div id="g2" class="itxst">
<div class="item" data-id="item1">item 1</div>
<div class="item" data-id="item2">item 2</div>
<div class="item" data-id="item3">item 3</div>
</div>
</div>
<div id="msg"></div>
<script>
const msg = document.getElementById("msg");
//第一组
const g1 = document.getElementById("g1");
const ops = {
animation: 1000,
delay: 1,
// 拖拽的元素
draggable: ".item",
// 选中的样式
chosenClass: "chosen",
//停靠位置样式
ghostClass: 'ghost',
forceFallback: true,
group: { name: 'itxst.com', pull: "clone", put: true },
// ******** 选中执行的方法 ********
onChoose(event) {
const index = event.oldIndex
msg.innerHTML = `选中了第${arr1[index]}`
},
// ******** 拖动结束 ********
onEnd(event) {
console.log(JSON.stringify(arr1));
}
}
const sortable = Sortable.create(g1, ops)
// 获取数据
const arr1 = sortable.toArray()
// 第二组
const g2 = document.getElementById("g2")
const ops2 = {
animation: 166,
delay: 1,
// 拖拽的元素
draggable: ".item",
// 选中的元素
chosenClass: "chosen",
// 停靠位置样式
ghostClass: "ghost",
// 禁用html5原生拖拽
forceFallback: true,
group: { name: 'itxst.com', pull: "clone", put: true },
// ******** 选中的方法 ********
onChoose(event) {
const index = event.oldIndex
msg.innerHTML = `选中第${arr2[index]}`
},
// ******** 拖动结束 ********
onEnd(event) {
console.log(JSON.stringify(arr2))
}
}
const sortable2 = Sortable.create(g2, ops2)
const arr2 = sortable2.toArray()
</script>
</body>
</html>