jQuery 可拖动选择具有较高 z-index 的元素后面的目标

发布于 12-14 21:48 字数 1025 浏览 4 评论 0原文

我正在使用 jQuery UI 可拖动。我有一堆带有 z 索引的元素和一个完全位于可拖动容器上方的元素。我无法将其设置为可拖动,因为单击事件无法到达它,因为它位于另一个元素后面。

如何访问该元素并让它响应鼠标事件?

我的 CSS:

#top {
    width:500px;
    height:500px;
    z-index:5;
    background-color:rgba(255,0,0,0.5);
    position:absolute;
}
#child {
    width:300px;
    height:300px;
    z-index:4;
    background-color:rgba(0,255,0,0.5);
    position:absolute;
    top:50px;
    left:50px;
}
#draggable {
     width:200px;
    height:200px;  
    z-index:4;
    background-color:rgba(0,0,255,0.5);
    position:absolute;
    top:10px;
    left:10px;
}

我的 HTML:

<div id="top"></div>
<div id="child">
    <div id="draggable"></div>
</div>

和我的 Javascript:

$(function(){

    $("#draggable").draggable({
        containment: "child"
    });

});

我还将所有这些代码放入 JS Fiddle 中进行演示: http://jsfiddle .net/wvSqk/

谢谢

I am using jQuery UI draggable. I have stack of elements with z-indexes and one element that is completely over the draggable container. I can't set it to draggable because click events aren't getting through to it because it is behind another element.

How can I access that element and get it to respond to the mouse events?

My CSS:

#top {
    width:500px;
    height:500px;
    z-index:5;
    background-color:rgba(255,0,0,0.5);
    position:absolute;
}
#child {
    width:300px;
    height:300px;
    z-index:4;
    background-color:rgba(0,255,0,0.5);
    position:absolute;
    top:50px;
    left:50px;
}
#draggable {
     width:200px;
    height:200px;  
    z-index:4;
    background-color:rgba(0,0,255,0.5);
    position:absolute;
    top:10px;
    left:10px;
}

My HTML:

<div id="top"></div>
<div id="child">
    <div id="draggable"></div>
</div>

And my Javascript:

$(function(){

    $("#draggable").draggable({
        containment: "child"
    });

});

I have also put all this code into JS Fiddle to demonstrate: http://jsfiddle.net/wvSqk/

Thanks

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

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

发布评论

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

评论(4

我们的影子2024-12-21 21:48:50

刚刚解决了这个问题,没有事件转发或任何复杂的事情 - 只需将另一个透明层放在所有内容之上,并将其标记为可拖动。每当拖动它时,都会将其位置复制到下面的目标图层。
像这样的事情:

var $draggableOverlay = $("#draggableOverlay");
var $draggableTarget = $('#draggableTarget');

$draggableOverlay.draggable({
    drag: function() {
        $draggableTarget.css('left', $draggableOverlay.css('left'));
        $draggableTarget.css('top', $draggableOverlay.css('top'));
    }
});

您可以应用更多调整来使其看起来更漂亮,并关闭一些边缘情况,但它确实有效。

Just solved this without event forwarding or anything complicated - Simply put another, transparent, layer on top of everything, and mark it as draggable. and whenever it is dragged copy its position to the target layer underneath.
Something like this:

var $draggableOverlay = $("#draggableOverlay");
var $draggableTarget = $('#draggableTarget');

$draggableOverlay.draggable({
    drag: function() {
        $draggableTarget.css('left', $draggableOverlay.css('left'));
        $draggableTarget.css('top', $draggableOverlay.css('top'));
    }
});

There are more tweaks you can apply to make it look nice, and close some edge cases, but it works.

金兰素衣2024-12-21 21:48:50

已经提出了与此类似的问题: css 'pointer -IE 的事件属性替代

现在我知道它并不完全相同,但它确实处理从一个元素传递到另一个元素的鼠标事件。查看那里的链接,看看它是否可以帮助您解决您的情况。

:)

There has been a question similar to this already asked: css 'pointer-events' property alternative for IE.

Now I know it's not exactly the same, but it does deal with mouse events being passed from one element to another. Check out the links there and see if it can help you out in your situation.

:)

余厌2024-12-21 21:48:50

另一种可能的解决方案是将draggable应用于最顶部的元素,并返回实际应该作为“helper”拖动的元素。请注意,您实际上不应该传递要拖动的元素本身 - 因为拖动完成时它会被删除 - 而应该传递它的副本;类似:

$( "#top" ).draggable({
  cursor: "move",
  cursorAt: { top: -12, left: -20 },
  helper: function( event ) {
     return $( "#draggable").clone();
  }
});

这当然只是一个开始(例如,原始元素应该被隐藏,并且需要计算“cursorAt”),但如果您需要对此的进一步指导,我很乐意提供帮助。

another possible solution is to apply the draggable to the top-most element, and return the element that should actually be dragged as a 'helper'. just note that you shouldn't actually pass the to-be-dragged element itself - as it gets removed when dragging completes - but a copy of it; something like:

$( "#top" ).draggable({
  cursor: "move",
  cursorAt: { top: -12, left: -20 },
  helper: function( event ) {
     return $( "#draggable").clone();
  }
});

this is just a start of course (e.g. the original element should be hidden, and 'cursorAt' needs to be calculated), but i'm happy to help if you need further guidance on this.

茶花眉2024-12-21 21:48:50

您必须将 #top div z-index 设置为低于 #child z-index

#top {
    ...
    z-index:3;
}

如果您无法更改 CSS,则可以在以下情况下使用 jquery 进行更改:鼠标位于#top div 上。

类似这样的事情

 var oldValue=  $("#top").css('z-index');
 var newValue = $("#child").css('z-index')-1;

 $("#top").hover(function(){$("#top").css('z-index',newValue)},
                   function(){$("#top").css('z-index',oldValue)});

有点麻烦,但你必须调整。 http://jsfiddle.net/wvSqk/4/

You have to put the #top div z-index to a lower than #child z-index

#top {
    ...
    z-index:3;
}

If you can't change the CSS, you can change it using jquery when the mouse is over #top div.

Something like that

 var oldValue=  $("#top").css('z-index');
 var newValue = $("#child").css('z-index')-1;

 $("#top").hover(function(){$("#top").css('z-index',newValue)},
                   function(){$("#top").css('z-index',oldValue)});

it's a little buggy, but you have to adjust. http://jsfiddle.net/wvSqk/4/

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