我想将一个元素拖到两个或更多可放置区域中,但这些可放置区域需要完全包含在我的可拖动对象中。
问题是,jQuery UI's existing functionality for droppable tolerances 都没有满足这个需求。
理想情况下,我会使用“相交”,但代码中可拖放对象的测量值是相反的。 (可以通过搜索 $.ui.intersect
在 jquery-ui.js
中找到此逻辑。)
我已经尝试通过 duck punching with jQuery 覆盖该函数并尝试将 tolerance
设置为这样的自定义函数:
tolerance: function(draggable, droppable) {
if(!droppable.offset) return false;
return ...logic check here...
},
drop: ...continues...
均无效。
这是一个 JSFiddle 来说明我的意思:https://jsfiddle.net/myingling/kgaqb0ay/5/
同样,一个人//covered//的所有 Items 都应该被分配。
最佳答案
修改 $.ui.intersect 似乎是最好的方法。你有不同的选择。如果您不需要那么大的灵 active ,您可以简单地添加一个tolerance 类型,例如“cover”。然后你只需要在开关中添加一个 case 来检查 intersect 中的公差类型,这可以恰好是 'fit' 的倒数。像这样:
case 'fit':
return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
break;
case 'cover':
return (l >= x1 && x2 >= r && t >= y1 && y2 >= b);
break;
参见:https://jsfiddle.net/6nyqja4a/4/
或者如果您想要更大的灵 active ,您可以添加一个案例,其中 tolerance 是一个函数。然后你可以在选项中传递一个函数,它可以让你对不同的 droppable 有精确的容忍度。例如这样的事情: 在交集函数中:
if (toleranceMode instanceof Function) {
return toleranceMode(draggable, droppable, x1, x2, y1, y2, l, r, t, b);
} else {
switch (toleranceMode) {
case 'fit':
return (l <= x1 && x2 <= r && t <= y1 && y2 <= b);
break;
...
你这样调用它:
$('.droppable').droppable({
hoverClass: "yellow",
tolerance: function(drag, drop, x1, x2, y1, y2, l, r, t, b) {
return (l >= x1 && x2 >= r && t >= y1 && y2 >= b);
},
drop: function(event, ui) {
$("#value_" + $(this).data("id")).val(ui.draggable.data("id"));
console.log("Item " + $(this).data("id") + " taken by " + ui.draggable.data("id"));
}
});
参见:https://jsfiddle.net/h4wm3r09/3/
从 jquery 1.12 开始,$.ui.intersect 函数被限定了范围,因此之后不能直接修改它。它在$.ui.ddmanager中作为局部变量被调用,所以即使你修改了$.ui.intersect,它也不会被使用。自定义它有点复杂。你可以这样做,基本上你重新确定 intersect 的范围,然后在 $.ui.ddmanager上重新定义 drag 和 drop 方法strong> 以便它调用修改后的相交:
var intersect = $.ui.intersect = ( function() {
function isOverAxis( x, reference, size ) {
return ( x >= reference ) && ( x < ( reference + size ) );
}
return function( draggable, droppable, toleranceMode, event ) {
if ( !droppable.offset ) {
return false;
}
var x1 = ( draggable.positionAbs ||
draggable.position.absolute ).left + draggable.margins.left,
y1 = ( draggable.positionAbs ||
draggable.position.absolute ).top + draggable.margins.top,
x2 = x1 + draggable.helperProportions.width,
y2 = y1 + draggable.helperProportions.height,
l = droppable.offset.left,
t = droppable.offset.top,
r = l + droppable.proportions().width,
b = t + droppable.proportions().height;
if (toleranceMode instanceof Function) {
return toleranceMode(draggable, droppable, x1, x2, y1, y2, l, r, t, b);
} else {
switch ( toleranceMode ) {
case "fit":
return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b );
case "intersect":
return ( l < x1 + ( draggable.helperProportions.width / 2 ) && // Right Half
x2 - ( draggable.helperProportions.width / 2 ) < r && // Left Half
t < y1 + ( draggable.helperProportions.height / 2 ) && // Bottom Half
y2 - ( draggable.helperProportions.height / 2 ) < b ); // Top Half
case "pointer":
return isOverAxis( event.pageY, t, droppable.proportions().height ) &&
isOverAxis( event.pageX, l, droppable.proportions().width );
case "touch":
return (
( y1 >= t && y1 <= b ) || // Top edge touching
( y2 >= t && y2 <= b ) || // Bottom edge touching
( y1 < t && y2 > b ) // Surrounded vertically
) && (
( x1 >= l && x1 <= r ) || // Left edge touching
( x2 >= l && x2 <= r ) || // Right edge touching
( x1 < l && x2 > r ) // Surrounded horizontally
);
default:
return false;
}
}
};
} )();
然后,您无需更改任何内容,只需以完全相同的方式重新定义它们即可。
$.ui.ddmanager.drag = function( draggable, event ) {
// If you have a highly dynamic page, you might try this option. It renders positions
// every time you move the mouse.
if ( draggable.options.refreshPositions ) {
$.ui.ddmanager.prepareOffsets( draggable, event );
}
// Run through all droppables and check their positions based on specific tolerance options
$.each( $.ui.ddmanager.droppables[ draggable.options.scope ] || [], function() {
if ( this.options.disabled || this.greedyChild || !this.visible ) {
return;
}
var parentInstance, scope, parent,
intersects = intersect( draggable, this, this.options.tolerance, event ),
c = !intersects && this.isover ?
"isout" :
( intersects && !this.isover ? "isover" : null );
if ( !c ) {
return;
}
if ( this.options.greedy ) {
// find droppable parents with same scope
scope = this.options.scope;
parent = this.element.parents( ":data(ui-droppable)" ).filter( function() {
return $( this ).droppable( "instance" ).options.scope === scope;
} );
if ( parent.length ) {
parentInstance = $( parent[ 0 ] ).droppable( "instance" );
parentInstance.greedyChild = ( c === "isover" );
}
}
// We just moved into a greedy child
if ( parentInstance && c === "isover" ) {
parentInstance.isover = false;
parentInstance.isout = true;
parentInstance._out.call( parentInstance, event );
}
this[ c ] = true;
this[ c === "isout" ? "isover" : "isout" ] = false;
this[ c === "isover" ? "_over" : "_out" ].call( this, event );
// We just moved out of a greedy child
if ( parentInstance && c === "isout" ) {
parentInstance.isout = false;
parentInstance.isover = true;
parentInstance._over.call( parentInstance, event );
}
} );
}
$.ui.ddmanager.drop = function( draggable, event ) {
var dropped = false;
// Create a copy of the droppables in case the list changes during the drop (#9116)
$.each( ( $.ui.ddmanager.droppables[ draggable.options.scope ] || [] ).slice(), function() {
if ( !this.options ) {
return;
}
if ( !this.options.disabled && this.visible &&
intersect( draggable, this, this.options.tolerance, event ) ) {
dropped = this._drop.call( this, event ) || dropped;
}
if ( !this.options.disabled && this.visible && this.accept.call( this.element[ 0 ],
( draggable.currentItem || draggable.element ) ) ) {
this.isout = true;
this.isover = false;
this._deactivate.call( this, event );
}
} );
return dropped;
}
https://jsfiddle.net/u6wfj8mj/1/
显然,这个更容易出错,可能有更好的方法来实现这一点。例如,通常您可以扩展小部件,这样会更干净。但是 intersect 和 ddmanager 都在 draggable 和 droppable 中使用,并不直接在这些小部件中使用。所以很难以干净的方式扩展。 您也可以将逻辑直接放在 draggables 和 droppables 的 drag 事件和 drop 事件中,但由于存在默认容差,不确定它是否更好。
关于javascript - 为 jQuery UI Droppable 的 Intersect tolerance 构建匹配选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46519613/