JQuery 拖动到父级之外

发布于 2024-08-29 04:44:50 字数 1029 浏览 4 评论 0原文

我正在使用 JQuery 的draggable();使 li 元素可拖动。唯一的问题是当元素被拖到其父元素之外时,它不会显示。也就是说,它不会离开其父 div 的范围。一个例子在这里: https://i.sstatic.net/jo6tC.png - 就像如果其他所有内容的 z-index 具有更高的优先级(并且该元素滑到所有内容下方)。

代码如下:

$('.photoList li').draggable({ 
        distance: 20,
        snap: theVoteBar,
        revert: true,
        containment: 'document'
    });

li 元素放置在 DIV 中,如下所示:

<div class="coda-slider preload" id="coda-slider-1">
    <div class="panel">
        <div class="panel-wrapper">
            <h2 class="title" style="display:none;">Page 1</h2>
            <ul class="photoList">
                <li>
                    <img class="ui-widget-content" src="photos/1.jpg" style="width:100px;height:100px;" />
                </li>
            </ul>
        </div>
    </div>

我确信问题是它不会离开其父容器,但我不知道该怎么做才能将其取出。

任何指导或帮助将不胜感激!

I'm using JQuery's draggable(); to make li elements draggable. The only problem is when the element is dragged outside its parent, it doesn't show up. That is, it doesn't leave the confines of its parent div. An example is here: https://i.sstatic.net/jo6tC.png - it's as if the z-index for everything else has greater priority (and the element slides under everything).

Here's the code:

$('.photoList li').draggable({ 
        distance: 20,
        snap: theVoteBar,
        revert: true,
        containment: 'document'
    });

And the li elements are placed in DIVs like this:

<div class="coda-slider preload" id="coda-slider-1">
    <div class="panel">
        <div class="panel-wrapper">
            <h2 class="title" style="display:none;">Page 1</h2>
            <ul class="photoList">
                <li>
                    <img class="ui-widget-content" src="photos/1.jpg" style="width:100px;height:100px;" />
                </li>
            </ul>
        </div>
    </div>

I'm positive the problem is it won't leave its parent container, but I'm not sure what to do to get it out.

Any direction or help would be appreciated GREATLY!

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

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

发布评论

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

评论(5

囍笑 2024-09-05 04:44:51

我带着完全相同的问题登陆此页面,布伦丹的回答让我很接近。设置 appendTo 选项没有帮助,但我可以通过将 overflow:visible 添加到可拖动元素的父级来解决我的问题。事实证明,我是从链条上游的某个地方草率地继承了隐藏的东西。

旁注,看起来这是可行的,因为 jQuery UI 通过动态设置相对于父级的定位来移动可拖动元素 - 这对我来说是新的英特尔!

I landed on this page with the exact same problem, and Brendan's answer got me close. Setting the appendTo option didn't help, but I was able to resolve my issue by adding overflow: visible to the parent of my draggable element. Turns out I was sloppily inheriting hidden from somewhere up the chain.

Side note, it looks like this works because jQuery UI moves the draggable element by dynamically setting positioning relative to the parent - this was new intel for me!

黯然#的苍凉 2024-09-05 04:44:51

我也遇到了同样的问题。
您可以使用此选项

包含:$('#divmain')
helper: 'clone'

分别用于
- 定义下降动作的限制区域
- for 显示可见的拖动对象也在 div 父级之外但在 #divmain

示例

<div id="divmain">
  <div id="divparentdraggable">
    <div id="divdraggable"></div>
  </div>
  <div id="divparentdroppable">
    <div id="divdroppable"></div>
  </div>
</div>

jquery 代码内:

$('divparentdraggable').draggable({ 
...
containment: $('#divmain'),
helper: 'clone',
...
});

我希望这对某人有帮助。

I had also the same problem.
You can use this option

containment: $('#divmain')
helper: 'clone'

respectively for
- define limit area for dropping action
- for shows visible dragged object also outside div parent but inside #divmain

example

<div id="divmain">
  <div id="divparentdraggable">
    <div id="divdraggable"></div>
  </div>
  <div id="divparentdroppable">
    <div id="divdroppable"></div>
  </div>
</div>

jquery code:

$('divparentdraggable').draggable({ 
...
containment: $('#divmain'),
helper: 'clone',
...
});

I hope this help someone.

鸢与 2024-09-05 04:44:51

看起来上面的一些组合对我来说是这样的。

设置 appendTo: 'body'helper: 'clone'
这样,一个新的 DOM 对象将被创建为主体的子对象,该对象在任何地方都可见。这样您就可以将其拖放到您喜欢的任何地方。

Looks like a combination of some of the above did it for me.

Set appendTo: 'body' and helper: 'clone'.
This way a new DOM-Object will be created as child of the body which will be visible everywhere. This way you can drag (and drop) it everywhere you like.

北风几吹夏 2024-09-05 04:44:51

这看起来像是一个 CSS 问题。尝试关闭还原,以便当您将其拖动到半可见的位置时它保持原状。然后在 Firebug 中使用 z-index 等,看看是否可以显示它。这可能是父 ul 或 div 之一具有“溢出:隐藏”的 CSS 问题。

This looks like a css issue. Try turning off the revert so it stays put when you drag it to where it is half visible. Then play with the z-index etc in Firebug and see if you can get it showing. It might be a css issue with one of the parent ul or div's with 'overflow: hidden'.

莳間冲淡了誓言ζ 2024-09-05 04:44:51

我遇到了类似的问题,并通过设置选项解决了它:

appendTo: 'body'

唯一的副作用是,如果您根据其父容器将样式应用于该项目,这些样式将不会出现在助手上。

I had a similar issue and resolved it by setting the option:

appendTo: 'body'

The only side effect to this is if you have styles applied to the item based on it's parent container, those styles won't be present on the helper.

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