jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。拖拽(Drag and Drop)是一种用户界面交互方式,允许用户通过鼠标或触摸屏移动元素。
以下是一个使用 jQuery UI 实现多个 div
拖拽排序的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery UI Drag and Drop</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<style>
.draggable {
width: 100px;
height: 100px;
background-color: #ccc;
margin: 10px;
display: inline-block;
}
</style>
</head>
<body>
<div id="sortable">
<div class="draggable">1</div>
<div class="draggable">2</div>
<div class="draggable">3</div>
<div class="draggable">4</div>
<div class="draggable">5</div>
</div>
<script>
$(function() {
$("#sortable").sortable();
$("#sortable").disableSelection();
});
</script>
</body>
</html>
position
属性为 absolute
或 relative
。stack
选项来控制元素的堆叠顺序。transform
属性来优化性能,如 transform: translate3d(0, 0, 0);
。通过以上示例代码和解决方法,你应该能够实现并解决 jQuery 多个 div
拖拽的相关问题。
领取专属 10元无门槛券
手把手带您无忧上云