具有可放置、可拖动和可调整大小的 Jquery 无法按预期工作

发布于 2024-12-05 02:12:23 字数 750 浏览 0 评论 0原文

JQueryUI Draggable inside Droppable 且可调整大小不起作用。想法是能够将拖放的元素拖动到 div 并调整放置元素的大小。

工作代码: http://jsfiddle.net/lukaszjg/GvDLh/

重现问题的说明:

  1. 拖动“将我拖到“放置的地方”
    1. 尝试调整大小
    2. 尝试将拖动的元素拖动到“放置位置”内
    3. 再次尝试调整拖动元素的大小 - 未按预期工作

请查找下面的代码,其中 #dragme 和 #droppable 引用相应的 div。 有什么办法解决它吗?

$("#dragme").draggable({
helper: 'clone',
cursor: 'move',
tolerance: 'fit'

});

var x = null;
$("#droppable").droppable({
drop: function(e, ui) {

x = ui.helper.clone();

x.draggable({
helper: 'original',
containment: '#droppable',
tolerance: 'fit'
});

x.resizable();
x.appendTo('#droppable');
ui.helper.remove();
}
});

JQueryUI Draggable inside Droppable with resizable doesn't work. Idea is to be able drag elements which are dragged and dropped to div with resize for dropped elements.

Working code: http://jsfiddle.net/lukaszjg/GvDLh/

Instructions to reproduce problem:

  1. Drag "drag me" to the "place to drop"
    1. Try resize
    2. Try to drag dragged element inside "place to drop"
    3. Try resize again dragged element - is not working as expected

Please find code below where #dragme and #droppable refer to corresponding divs.
Any ide how to fix it ?

$("#dragme").draggable({
helper: 'clone',
cursor: 'move',
tolerance: 'fit'

});

var x = null;
$("#droppable").droppable({
drop: function(e, ui) {

x = ui.helper.clone();

x.draggable({
helper: 'original',
containment: '#droppable',
tolerance: 'fit'
});

x.resizable();
x.appendTo('#droppable');
ui.helper.remove();
}
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹微笑 2024-12-12 02:12:23

当您将可调整大小的小部件绑定到元素时,它将添加多个

元素。然后,在您的 drop 回调中,您将看到:

x = ui.helper.clone();

这将克隆 .ui-ressized-handle 元素以及您想要克隆的元素。然后几行之后,您会看到:

x.resizable();

显然,该调用因 .ui-resizeable-handle 元素的存在而变得混乱;它最终会添加一组额外的 .ui-resizeable-handle 元素,但它们可能无法工作,因为 z-index 问题:原件将(可能)位于它们之上并阻止所有事件从深入到附加了事件处理程序的 .ui-resiable-handle 元素开始。如果您在调整克隆大小之前手动删除有问题的

x.find('.ui-resizable-handle').remove();
x.resizable();

那么它就可以工作:

$("#droppable").droppable({
    drop: function(e, ui) {
        x = ui.helper.clone();
        x.draggable({
            helper: 'original',
            containment: '#droppable',
            tolerance: 'fit'
        });
        x.find('.ui-resizable-handle').remove();
        x.resizable();
        x.appendTo('#droppable');
        ui.helper.remove();
    }
});

更新的小提琴:http://jsfiddle.net/ambiguously/xZECa/

仅调用 x.ressized('destroy') 来清理它是行不通的,因为x 无法调整大小,因此什么也不会发生。

应该有更好的方法来完成这项工作,但我还不知道它是什么。

When you bind a resizable widget to an element, it will add several <div class="ui-resizable-handle"> elements. Then, inside your drop callback, you have this:

x = ui.helper.clone();

That will clone the .ui-resizable-handle elements as well as the elements that you want to clone. And then a couple lines later, you have:

x.resizable();

Apparently that call gets confused by the presence of the .ui-resizeable-handle elements; it will end up adding an extra set of .ui-resizeable-handle elements but they won't work probably because of z-index issues: the originals will (probably) be above them and blocking all the events from getting down to the .ui-resiable-handle elements that have event handlers attached to them. If you manually remove the offending <div>s before make the clone resizable:

x.find('.ui-resizable-handle').remove();
x.resizable();

Then it works:

$("#droppable").droppable({
    drop: function(e, ui) {
        x = ui.helper.clone();
        x.draggable({
            helper: 'original',
            containment: '#droppable',
            tolerance: 'fit'
        });
        x.find('.ui-resizable-handle').remove();
        x.resizable();
        x.appendTo('#droppable');
        ui.helper.remove();
    }
});

Updated fiddle: http://jsfiddle.net/ambiguous/xZECa/

Just calling x.resizable('destroy') to clean it up doesn't work because x isn't resizable so nothing happens.

There should be a better way to make this work but I don't know what it is yet.

久隐师 2024-12-12 02:12:23

我在尝试您的解决方案时遇到了一些麻烦,但您让我陷入了困境,所以如果这对其他人有帮助,我将描述我的问题和解决方案。

我的案例:一个计划,其中可放置的 div 代表每个员工,可拖动、可调整大小的 div 代表任务。

问题:完全相同,只是我的“任务”已经是可删除的子项(来自数据库)并且有很多 .data() 内容,因此克隆很混乱。
可放置的放置事件会抛出错误“无法转换 javascript 参数”并破坏我的应用程序,阻止任何进一步的拖动。

在看到您的解决方案并未能在我的案例中重现它之后,我回到了基础知识:

“如果您有伐木工智商,请使用斧头”。

所以这就是内容:

$("#dragme").draggable({
helper: 'original', //Needed in my case
cursor: 'move',
tolerance: 'fit',

start: function(event,ui){
$(this).resizable( "destroy" );/* The resizable generates troubles to the droppable?
Well let's remove the problem...
*/
} 

}).resizable({
/*All my functions to apply to the task when resized*/

});// I'm creating the resizable immediately because needed to attribute more or less time to the task 


$("#droppable").droppable({
drop: function(e, ui) {
/*All my drop functions to manage the task*/

//creating the resizable again PROBLEM SOLVED
$(ui.draggable).resizable({
/*All my functions to apply to the task when resized*/
});

}
});

希望这可以帮助

注意:不是很我确信我的英语水平,所以如果出现错误或拼写错误,请参阅“回到基础知识”部分并原谅我;-)。

I had some troubles experimenting your solution but you put me on the way so i'll describe my problem and solution if this helps someone else.

My case: A planning with droppable divs representing each employee, and draggable, resizable divs representing tasks.

Problem: Quite the same, only that my "tasks" are already children of the droppable (comes from a database) and have a lot of .data() stuff so cloning was a mess.
The droppable drop event throw an error "can't convert javascript argument" and ruins my app blocking any further drag.

After seeing your solution and fail to reproduce it in my case i went back to basics:

"If you got a lumberjack IQ use an Ax".

So here's the stuff:

$("#dragme").draggable({
helper: 'original', //Needed in my case
cursor: 'move',
tolerance: 'fit',

start: function(event,ui){
$(this).resizable( "destroy" );/* The resizable generates troubles to the droppable?
Well let's remove the problem...
*/
} 

}).resizable({
/*All my functions to apply to the task when resized*/

});// I'm creating the resizable immediately because needed to attribute more or less time to the task 


$("#droppable").droppable({
drop: function(e, ui) {
/*All my drop functions to manage the task*/

//creating the resizable again PROBLEM SOLVED
$(ui.draggable).resizable({
/*All my functions to apply to the task when resized*/
});

}
});

Hope this can help

Note: Not very sure of my English level so in case of mistakes o misspelling please refer to the "Back to basics" section and forgive me ;-).

傾城如夢未必闌珊 2024-12-12 02:12:23

一旦你将“.ressized()”附加到元素上,首先销毁事件然后附加。
它会起作用的。
//$(ele) 是一个对象。



    $(ele).resizable().resizable("destroy");
        $(ele).draggable();
        $(ele).resizable({
          animate: 'true',
          ghost: 'true',
          handles: 'ne, nw, se, sw',
    });


Once you attach ".resizable()" with element first destroy the event and then attach.
it will work.
//$(ele) is an object.



    $(ele).resizable().resizable("destroy");
        $(ele).draggable();
        $(ele).resizable({
          animate: 'true',
          ghost: 'true',
          handles: 'ne, nw, se, sw',
    });


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文