【问题标题】:Move a DIV under a another DIV via DRAGGABLE?(CSS, JQuery)通过 DRAGGABLE 将一个 DIV 移动到另一个 DIV 下?(CSS,JQuery)
【发布时间】:2015-04-05 21:14:28
【问题描述】:

我有两个不同的图像,我将它们堆叠在一起。 .image-1 是背景图片,.image-2 是前景图片。我想要这样,因为前景图像是一个包含整个内部的 PNG 文件。当您查看整个图像时,您会看到背景图像的一部分。我希望能够通过 JQuery 中的可拖动来移动背景图像。这不起作用,因为我无法触摸背景层,因为它被前景层覆盖。这是我的源代码:

CSS

.image-1
{
    z-index: 0;
    position: absolute;
    background-image: url("image_1.png");
    width: 400px;
    height: 400px;
    background-repeat: no-repeat;
    background-position: center;
}

.image-2
{
    z-index: 1;
    position: absolute;
    background-image: url("image_2.png");
    width: 400px;
    height: 400px;
    background-repeat: no-repeat;
    background-position: center;
}

HTML

<div class="image-1"></div>
<div class="image-2"></div>

JQuery

$(".image-1").draggable({});

如何通过可拖动访问背景 div,但仍将我移动的图像保留在背景中?

已编辑:

我试过这样:

$("#image-2").mousedown(function(event)
{
    $("#image-1").draggable({ });
});

但它不起作用。

【问题讨论】:

标签: javascript jquery css html


【解决方案1】:

从较高层获取事件并将其传递给较低层。不要在每次 mousedown 时触发 draggable,而是将拖动事件传递给下层。

$(".image-1").draggable({});
$(".image-2").mousedown(function(event){
    $(".image-1").trigger(event);
});
.image-1
{
    z-index: 0;
    position: absolute;
    background:#444444;
    width: 400px;
    height: 400px;
    background-repeat: no-repeat;
    background-position: center;
}

.image-2
{
    z-index: 1;
    position: absolute;
    background:#ff0000;
    width: 400px;
    height: 400px;
    background-repeat: no-repeat;
    background-position: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/jquery-ui-git.js"></script>
<div class="image-1"></div>
<div class="image-2"></div>

【讨论】:

  • 感谢您的帮助!我试过这样。我编辑了我的帖子,所以你可以看到代码,很遗憾它没有工作。
  • 对不起。我将 ID 选择器更改为类选择器。我建议对某些元素使用 ID 而不是类。
  • 很抱歉,它仍然无法正常工作。 :( 这不是 draggabke 函数,当我切换类的 z-indexes 时,可拖动的工作非常好,这意味着我可以用鼠标移动 div。谢谢。
  • 我已经更新了我的答案。请运行代码 sn -p 并再次检查
  • 它现在正在工作,完全符合我的意愿!非常感谢您的宝贵时间,您很棒。祝你有美好的一天;)
猜你喜欢
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2016-11-05
  • 1970-01-01
  • 2023-04-03
  • 2012-07-17
相关资源
最近更新 更多