在可拖动元素上设置z-index

35

我正在尝试使用jQuery设置可拖动元素的z-index。您可以在此处查看我正在讨论和完成的内容:

http://jsfiddle.net/sushik/LQ4JT/1/

这很原始,并存在问题。您有什么想法,如何使最后点击的元素具有最高的z-index,并且而不是将所有其他元素重置为基本的z-index,使它们递增,因此倒数第二个点击的元素具有第二高的z-index等等。

我遇到的另一个问题是,它只在完全点击事件上起作用,但是可拖动功能是通过单击并按住下键来工作的。 我该如何在初始点击下应用类,而不是等待释放点击的事件?

6个回答

59

您只需要使用draggable({stack: "div"}),现在当您拖动一个

时它会自动置于顶部。

请查看工作示例:http://jsfiddle.net/LQ4JT/8/


但这只在拖动时有效,点击时无效,对吗? - Mahesh
1
你必须拖动它才能使堆叠功能生效。这就是拖拽堆栈背后的全部意义。如果 z-index 是通过点击完成,则当您将其他行为绑定到单击时但不想将 div 带到前面时,可能会变得非常恼人。使用堆栈时所需做的只是拖动 div 1 像素,它就会被带到前面。 - Hussein
嗯...我刚刚收到了来自Jquery UI开发团队的邮件。在jquery-ui-1.9中,甚至“stack”选项也将被移除。 - Mahesh
2
我可以确认在1.11版本中,“stack”仍然可用。目前正在使用它。 - razorsyntax
2
针对如何使元素在单击而非拖动时也能够吸附到前面的问题,只需添加以下参数即可:distance: 0 - Keyslinger
显示剩余5条评论

27

我已经更新了你的CSS和Javascript。除非你非常绝望,否则不要在CSS中使用“!important”。

http://jsfiddle.net/LQ4JT/7/

    $(document).ready(function() {
        var a = 3;
        $('#box1,#box2,#box3,#box4').draggable({
            start: function(event, ui) { $(this).css("z-index", a++); }
        });
    $('#dragZone div').click(function() {
        $(this).addClass('top').removeClass('bottom');
        $(this).siblings().removeClass('top').addClass('bottom');
        $(this).css("z-index", a++);
    });

虽然此答案可行,但在 JavaScript 中有 2^31−1 的最大限制。 更多信息请参见 What is JavaScript's highest integer value that a Number can go to without losing precision?


7
我发现解决这个问题最简单的方法是使用appendTo和zIndex选项。
$('#box1,#box2,#box3,#box4').draggable({
  appendTo: "body",
  zIndex: 10000
});

6
以下代码可以满足您的要求。您需要将
堆叠起来,而不是设置z-index,并且您需要在单击而不是拖动后将
显示在顶部。

因此,对于堆叠,您需要使用stack: "div",对于通过单击显示

元素在顶部,您需要使用distance: 0

默认情况下,值为distance: 10,这意味着除非您将其拖动10像素,否则它不会显示在顶部。通过将值设置为distance: 0,可以使其在简单单击后显示在顶部。

尝试以下代码。

$('#box1,#box2,#box3,#box4').draggable({
    stack: "div",
    distance: 0
});

JSFiddle 实例


编辑:

点击下面的 运行代码片段 按钮执行此嵌入代码片段。

$(document).ready(function() {

  $('#box1,#box2,#box3,#box4').draggable({
    stack: "div",
    distance: 0
  });

  $('#dragZone div').click(function() {
    $(this).addClass('top').removeClass('bottom');
    $(this).siblings().removeClass('top').addClass('bottom');

  });
});
#box1 {
  width: 150px;
  height: 150px;
  background-color: red;
  position: absolute;
  top: 0px;
  left: 0px;
  z-index: 0
}

#box2 {
  width: 150px;
  height: 150px;
  background-color: green;
  position: absolute;
  top: 20px;
  left: 50px;
  z-index: 0
}

#box3 {
  width: 150px;
  height: 150px;
  background-color: yellow;
  position: absolute;
  top: 50px;
  left: 100px;
  z-index: 0
}

#box4 {
  width: 150px;
  height: 150px;
  background-color: blue;
  position: absolute;
  top: 70px;
  left: 200px;
  z-index: 0
}

.top {
  z-index: 100!important;
  position: relative
}

.bottom {
  z-index: 10!important;
  position: relative
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<div id="dragZone">
  <div id="box1"></div>
  <div id="box2"></div>
  <div id="box3"></div>
  <div id="box4"></div>
</div>


1

堆栈:“div” 也适用于元素组

<style media="screen">
    .box {width: 150px; height: 150px; position: absolute;  z-index: 0;}
    .box:nth-child(1) {background-color: red; top: 0px; left: 0px;}
    .box:nth-child(2) {background-color: green; top: 20px; left: 50px;}
    .box:nth-child(3) {background-color: yellow; top: 50px; left: 100px;}
    .box:nth-child(4) {background-color: blue; top: 80px; left: 150px;}
</style>
<script type="text/javascript">
    $(document).ready(function() {
        $('.box').draggable({stack: "div"});
    });
</script>
<div id="dragZone">
    <div class="box" id="box1"></div>
    <div class="box" id="box2"></div>
    <div class="box" id="box3"></div>
    <div class="box" id="box4"></div>
</div>

0

这里是Mahesh答案的大大简化版本:

$(document).ready(function() {
  var a = 1;
  $('#box1,#box2,#box3,#box4,#box5,#box6,#box7').draggable({
    start: function(event, ui) { $(this).css("z-index", a++); }
  });
});

http://jsfiddle.net/LQ4JT/713/

看起来还是很正常的,除非我漏掉了什么。


那个方法在拖动元素时有效,但仅仅点击它是无效的。Mahesh的回答更加完整。 - pmont

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接