简体   繁体   English

可调整大小的容器中可拖动元素的收容问题

[英]Containment issue with draggable element within a resizable container

I thought this would be simple but there appears to be a few bugs in jQuery UI when placing a draggable element within a resizable container. 我以为这很简单,但是当在可调整大小的容器中放置可拖动元素时,jQuery UI中似乎存在一些错误。

The following code allows the draggable element to be placed slightly outside the container because of the extra height created by the resize handle ( jsfiddle here ). 下面的代码允许将可拖动元素放置在容器的外部,因为调整大小手柄会产生额外的高度( jsfiddle here )。

HTML 的HTML

<div class="container2">
    <div class="item2"></div>
</div>

CSS 的CSS

.container2 {
    min-width:300px;
    min-height:200px;
    outline:1px solid black;
}
.item2 {
    width:50px;
    height:50px;
    background-color:red;
}

jQuery jQuery的

$(".container2").resizable();

$(".item2").draggable({
    containment: ".container2"
}).resizable({
    containment: ".container2"
});

So I got around this by setting the containment as the coordinates of the container using the following code : 所以我通过使用以下代码将容器设置为容器的坐标来解决此问题:

$(".item2").draggable({
    start: function (event, ui) {
        var containmentX1 = $(".container2").offset().left;
        var containmentY1 = $(".container2").offset().top;
        var containmentX2 = ($(".container2").outerWidth() + $(".container2").offset().left - $(this).outerWidth())
        var containmentY2 = ($(".container2").outerHeight() + $(".container2").offset().top - $(this).outerHeight())
        $(this).draggable({
            containment: [containmentX1, containmentY1, containmentX2, containmentY2]
        });
    },
}).resizable({
    containment: ".container2"
});

This works fine except for one problem... the containment coordinates are only set the second time the draggable element is moved. 除一个问题外,此方法工作正常。仅在第二次移动可拖动元素时设置容纳坐标。

I used create rather than start to get around this, but then the coordinates are not changed if the container is resized. 我使用create而不是开始解决这个问题,但是如果调整容器大小,则不会更改坐标。

Is there a way to get the coordinates recognised as the containment area on first drag.? 有没有办法在第一次拖动时将坐标识别为容纳区域?

if following your approach you can set containment when resizing '.container2' and when initializing look there 如果按照你的方法,你可以调整大小“.container2'和初始化时的样子时,设定遏制

Another solution could be just to use css: 另一个解决方案可能只是使用CSS:

 .container2 .ui-resizable-s
{
    bottom:0;
}
.container2 .ui-resizable-e
{
    right:0;
}

you can verify there , hope this helps 您可以在那里验证,希望对您有所帮助

you can wrap your containment recalculation into a separate function and call it after initializing draggable (on create) and each time container gets resized (om stop) 您可以将遏制重新计算包装到单独的函数中,并在初始化可拖动对象(创建时)以及每次调整容器大小(停止时)后调用它

$(".container2").resizable({
stop: reCalcBounds
});

$(".item2").draggable({
    create: reCalcBounds
}).resizable({
    containment: ".container2"    
});

function reCalcBounds(){
 var $item =  $(".item2");
 $container = $(".container2");
 var containmentX1 = $container.offset().left;
    var containmentY1 = $container.offset().top;
    var containmentX2 = ($container.outerWidth() + $container.offset().left - $item.outerWidth())
    var containmentY2 = ($container.outerHeight() + $container.offset().top -$item.outerHeight())
    $item.draggable("option", "containment", [containmentX1, containmentY1, containmentX2, containmentY2]);

}

see updated fiddle 看到更新的小提琴

I think you also need to take care of changing minWidth and minHeight of the resizable container depending on position of the draggable item, you can use "stop" callaback of the draggable for that 我认为您还需要根据可拖动项的位置来更改可调整大小容器的minWidth和minHeight,您可以为此使用“停止”可拖动对象的回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM