<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI 拖动(Draggable) + 排序(Sortable)</title>
<link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
<script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="jquery-ui.min.js"></script>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
margin-bottom: 10px;
}
li {
margin: 5px;
padding: 5px;
width: 150px;
}
#sortable li {
height: 100px;
}
</style>
<script>
$(function() {
$("#sortable").sortable({
revert: true,
placeholder: "ui-state-highlight",
start: function(event, ui) {},
change: function(event, ui) {
console.log(ui.item.attr('data-number'));
console.log(ui.item.next().attr('data-number'))
}
});
$("#draggable").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid",
change: function(event, ui) {
console.log(ui.item.attr('data-number'));
console.log(ui.item.next().attr('data-number'))
}
});
// $(window).scroll(function(e) {
// $( "#sortable").sortable( "refresh" ); //触发滚动时刷新位置信息
// });
});
</script>
</head>
<body>
<ul>
<li id="draggable" class="ui-state-highlight">请拖拽我</li>
</ul>
<ul id="sortable" style="position: relative;">
<li class="ui-state-default" data-number="1">Item 1</li>
<li class="ui-state-default" data-number="2">Item 2</li>
<li class="ui-state-default" data-number="3">Item 3</li>
<li class="ui-state-default" data-number="4">Item 4</li>
<li class="ui-state-default" data-number="5">Item 5</li>
<li class="ui-state-default" data-number="6">Item 6</li>
<li class="ui-state-default" data-number="7">Item 7</li>
<li class="ui-state-default" data-number="8">Item 8</li>
<li class="ui-state-default" data-number="9">Item 9</li>
<li class="ui-state-default" data-number="10">Item 10</li>
<li class="ui-state-default" data-number="11">Item 11</li>
<li class="ui-state-default" data-number="12">Item 12</li>
<li class="ui-state-default" data-number="13">Item 13</li>
</ul>
</body>
</html>