简体   繁体   English

jQuery Stackable在动态创建的可拖动元素上不起作用

[英]jQuery stackable not working on dynamically created draggable elements

I'm creating draggable elements on runtime and want to make them stackable. 我正在运行时创建可拖动元素,并希望使其可堆叠。 Every time I generate a new draggable element, this code runs: 每当我生成一个新的可拖动元素时,此代码就会运行:

$('.draggable#Element' + ElementID).draggable({
    containment: "body",
    stack: "#draggables"
});

Draggable Container with two generated elements: 具有两个生成元素的可拖动容器:

<div id="draggables">
    <div class="draggable" id="Element1"></div>
    <div class="draggable" id="Element2"></div>
</div>

I can drag around every element. 我可以拖动每个元素。 The containmant also works. 容器也可以使用。

But: Unfortunately, only the last created element stacks if I drag it around. 但是:不幸的是,如果我拖拽它,只有最后创建的元素会堆叠。 The other elements keep their z-index and stay in background. 其他元素保留其z-index并保留在背景中。

I also tried to run this code everytime a new element is created (same result as the code above): 每当创建新元素时,我也尝试运行此代码(与上面的代码相同的结果):

$('.draggable').each(function() {
    $(this).draggable({
        containment: "body",
        stack: "#draggables"
    });
});

And this one: 还有这个:

$('.draggable').draggable({
    containment: "body",
    stack: "#draggables"
});

How do I achieve that every element stacks correctly? 如何实现每个元素正确堆叠?

jsFiddle Example: https://jsfiddle.net/zurgs1zb/ jsFiddle示例: https ://jsfiddle.net/zurgs1zb/

check first comment. 检查第一条评论。 and try delete all stack option and declaration stack option run time in after intialization: 并尝试在初始化后删除所有堆栈选项和声明堆栈选项的运行时:

$('.draggable').each(function() {
if($(this).draggable( "option", "stack" ) == false) //if not already set
   {
$( $(this) ).draggable( "option", "stack", "#draggables" ); // set stack option
   }

});

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

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