我正在使用 CSS 转换缩放一个 JQuery 可排序元素。可排序元素的起始位置和拖动时的偏移量都是错误的,因为 JQuery 没有考虑 CSS 比例。我使用此处找到的代码部分解决了它:
jQuery Drag/Resize with CSS Transform Scale
但我无法解决的是拖动开始时的可排序元素位置。它跳起来并向右跳了一下。我不知道要将什么放入启动事件处理程序:
start: function(e, ui)
{
// something is needed here to fix the initial offset
}
这个 fiddle 显示了问题: http://jsfiddle.net/adrianrosca/zbvLp/4/
最佳答案
draggable 的一个区别是transform 不是在元素本身上,而是在父元素上。所以它改变了一点逻辑。
这是针对此特定情况的解决方案,但您会发现它可能会根据情况发生变化。例如,如果您更改 transform-origin,或者如果您有水平排序,则必须对其进行调整。但逻辑保持不变:
var zoomScale = 0.5;
$(".container")
.sortable({
sort: function(e, ui) {
console.log(ui.position.left)
var changeLeft = ui.position.left - ui.originalPosition.left;
// For left position, the problem here is not only the scaling,
// but the transform origin. Since the position is dynamic
// the left coordinate you get from ui.position is not the one
// used by transform origin. You need to adjust so that
// it stays "0", this way the transform will replace it properly
var newLeft = ui.originalPosition.left + changeLeft / zoomScale - ui.item.parent().offset().left;
// For top, it's simpler. Since origin is top,
// no need to adjust the offset. Simply undo the correction
// on the position that transform is doing so that
// it stays with the mouse position
var newTop = ui.position.top / zoomScale;
ui.helper.css({
left: newLeft,
top: newTop
});
}
});
http://jsfiddle.net/aL4ntzsh/5/
编辑:
以前的答案适用于定位,但正如 Decent Dabbler 所指出的,交集函数存在一个缺陷,该函数验证何时应该发生排序。基本上,位置计算是正确的,但元素保留未转换的 width 和 height 值,这是导致问题的原因。 您可以通过在开始事件中修改这些值来调整这些值,以将比例因子考虑在内。例如:
start: function(e, ui) {
var items = $(this).data()['ui-sortable'].items;
items.forEach(function(item) {
item.height *= zoomScale;
item.width *= zoomScale;
});
}
关于jquery - 应用 CSS3 缩放时,可排序行为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24970789/