通过DRAGGABLE在另一个DIV下移动一个DIV?(CSS,JQuery)

问题描述:

我有两个不同的图像,我互相堆叠在一起。 .image-1是背景图像,.image-2是前景图像。我想要这样,因为前景图像是一个包含整个里面的PNG文件。当你看完整个你看到的背景图像的一部分。我希望能够通过JQuery中的可拖动来移动背景图片。这是行不通的,因为我不能触摸背景层,因为它被前景层覆盖。这是我的源代码:通过DRAGGABLE在另一个DIV下移动一个DIV?(CSS,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({ }); 
}); 

,但没有奏效。

+0

参考这个问题是,你想要什么 http://*.com/questions/11271704/jquery-drag-div-css-background。 – 2015-04-05 21:47:47

从高层获取事件并将其传递到低层。不要在每个mousedown上触发可拖动,而是将拖动事件传递到较低层。

$(".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>

+0

谢谢你的帮助!我试过这样。我编辑了我的文章,以便您可以看到代码,但不幸的是没有工作。 – user3877230 2015-04-05 21:31:29

+0

对不起。我将ID选择器更改为类Selector。我建议使用ID来代替某些元素的类。 – 2015-04-05 21:33:54

+0

我很抱歉,但它仍然无法正常工作。 :(这不是draggabke函数,当我切换类的拖动工作的Z索引完美的罚款,意味着我可以移动div与我的鼠标左右,谢谢。 – user3877230 2015-04-05 21:37:58