jQuery UI Sortable 是一个交互组件,允许用户通过拖拽对列表中的元素进行重新排序。它通常用于创建可排序的列表、网格或其他可重新排列的元素集合。
当尝试对浮动(floating)的 DIV 元素(使用 float: left
或 float: right
CSS 属性)使用 Sortable 功能时,可能会遇到排序行为不正常的问题。
position: relative
),这可能与浮动布局产生冲突。$("#sortable").sortable({
// sortable 配置
});
#sortable {
display: flex; /* 替代 float */
flex-wrap: wrap;
/* 或者使用 grid */
/* display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); */
}
$("#sortable").sortable({
tolerance: "pointer",
helper: "clone",
opacity: 0.8
});
#sortable {
overflow: hidden; /* 清除浮动 */
}
#sortable div {
float: left;
margin: 5px;
width: 100px;
height: 100px;
position: relative; /* 添加相对定位 */
}
$("#sortable").sortable({
placeholder: "sortable-placeholder",
helper: function(e, item) {
var helper = item.clone();
helper.css({
"width": item.width(),
"float": "none", // 移除浮动
"position": "absolute"
});
return helper;
}
});
.sortable-placeholder {
background: #f0f0f0;
border: 1px dashed #ccc;
visibility: visible !important;
float: none !important;
position: relative !important;
}
优势:
局限:
如果项目允许使用现代技术,可以考虑:
希望这些解决方案能帮助您解决 jQuery UI Sortable 与浮动 DIV 的兼容性问题。
没有搜到相关的文章