gpt4 book ai didi

javascript - 拖动图像下方的元素

转载 作者:行者123 更新时间:2023-11-28 02:23:03 24 4
gpt4 key购买 nike

我正在使用 JQuery draggable 制作 DnD 应用程序,但我遇到了这个问题。当我将我的“框”拖到图像中时,它们会在图像下方,因此您看不到它们。这是我的 html 和 css:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div class="content">

<div>
<div id="item1" class="item item1 ui-widget-content">
</div>
<div id="item2" class="item item2 ui-widget-content">
</div>
<div id="item3" class="item item3 ui-widget-content">
</div>
<div id="item4" class="item item4 ui-widget-content">
</div>
<div id="item5" class="item item5 ui-widget-content">
</div>
<div id="item6" class="item item6 ui-widget-content">
</div>
<div id="item7" class="item item7 ui-widget-content">
</div>
<div id="item8" class="item item8 ui-widget-content">
</div>


<br><br><br><br><br><br>
<div class="dash">
<img src="image1.png">

<div id="slot1" class="ui-widget-header">
<p>holder</p>
</div>

<div id="slot2" class="ui-widget-header">
<p>holder</p>
</div>

<div id="slot3" class="ui-widget-header">
<p>holder</p>
</div>

<div id="slot4" class="ui-widget-header">
<p>holder</p>
</div>

<div id="slot5" class="ui-widget-header">
<p>holder</p>
</div>

<div id="slot6" class="ui-widget-header">
<p>holder</p>
</div>

<div id="slot7" class="ui-widget-header">
<p>holder</p>
</div>

<div id="slot8" class="ui-widget-header">
<p>holder</p>
</div>

</div>
</div>



</body>
</html>
.content { position: relative; }

.item {
width: 65px;
height: 65px;
padding: 5px;
float: left;
margin: 0 10px 10px 0;
font-size: .9em;
display: inline-block;
}

.item1 {background: darksalmon;}
.item2 {background: cyan;}
.item3 {background: darkgreen;}
.item4 {background: lightpink;}
.item5 {background: lightgreen;}
.item6 {background: brown;}
.item7 {background: purple;}
.item8 {background: orange;}

.dash {
position: relative;
}


.ui-widget-header, .ui-widget-content { margin: 0; display: inline-block; margin: 10px;}

.ui-widget-header{
background: lightgrey;
border: solid;
}


#slot1 { height: 75px; width: 75px; position: absolute; top: 23px; left:37px; }
#slot2 { height: 75px; width: 75px; position: absolute; top: 23px; left:129px;}
#slot3 { height: 75px; width: 75px; position: absolute; top: 55px; left:445.5px;}
#slot4 { height: 75px; width: 75px; position: absolute; top: 233px; left:571.2px;}
#slot5 { height: 75px; width: 75px; position: absolute; top: 233px; left:656px;}
#slot6 { height: 75px; width: 75px; position: absolute; top: 233px; left:742px;}
#slot7 { height: 75px; width: 75px; position: absolute; top: 233px; left:828px;}
#slot8 { height: 75px; width: 75px; position: absolute; top: 197.5px; left:9px; }

在此代码笔中,您可以看到它们位于“holders”框下方: https://codepen.io/gezzzz/pen/ZNapaQ

我尝试了所有我能想到的调整 CSS 的方法,但我无法找出导致这种情况的原因

我该如何解决这个问题?

最佳答案

要解决此问题,您需要将 .item 元素上的 z-index 设置为高于 #slotN 元素上的值。默认情况下,它们设置为 1000

另请注意,在您使用元素的唯一 ID 的 JS 和 CSS 中有很多不必要的重复。而是将通用类放在它们上面,然后通过这些类进行选择。试试这个:

$(function() {
const correct = [{
slot: "slot1",
item: "item3"
}, {
slot: "slot2",
item: "item5"
}, {
slot: "slot3",
item: "item2"
}, {
slot: "slot4",
item: "item7"
}, {
slot: "slot5",
item: "item7"
}, {
slot: "slot6",
item: "item4"
}];

var answers = [{
slot1: ""
}, {
slot2: ""
}]

$(".item").draggable({
cursor: "pointer"
});

$('.slot').droppable({
drop: handleDropEvent
});
});


function handleDropEvent(event, ui) {
var item = ui.draggable;
var slot = $(this).attr('id');
ui.draggable.position({ of: $(this),
my: 'left top',
at: 'left top'
});
}
.content {
position: relative;
}

.item, .slot {
margin: 0;
display: inline-block;
margin: 10px;
}

.item {
width: 65px;
height: 65px;
padding: 5px;
float: left;
margin: 0 10px 10px 0;
font-size: 0.9em;
display: inline-block;
z-index: 10001;
}

.item1 { background: darksalmon; }
.item2 { background: cyan; }
.item3 { background: darkgreen; }
.item4 { background: lightpink; }
.item5 { background: lightgreen; }
.item6 { background: brown; }
.item7 { background: purple; }
.item8 { background: orange; }

.dash {
position: relative;
}

.slot {
background: lightgrey;
border: solid;
height: 75px;
width: 75px;
position: absolute;
}

#slot1 {
top: 23px;
left: 37px;
}

#slot2 {
top: 23px;
left: 129px;
}

#slot3 {
top: 55px;
left: 445.5px;
}

#slot4 {
top: 233px;
left: 571.2px;
}

#slot5 {
top: 233px;
left: 656px;
}

#slot6 {
top: 233px;
left: 742px;
}

#slot7 {
top: 233px;
left: 828px;
}

#slot8 {
top: 197.5px;
left: 9px;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="content">
<div>
<div id="item1" class="item item1 ui-widget-content"></div>
<div id="item2" class="item item2 ui-widget-content"></div>
<div id="item3" class="item item3 ui-widget-content"></div>
<div id="item4" class="item item4 ui-widget-content"></div>
<div id="item5" class="item item5 ui-widget-content"></div>
<div id="item6" class="item item6 ui-widget-content"></div>
<div id="item7" class="item item7 ui-widget-content"></div>
<div id="item8" class="item item8 ui-widget-content"></div>
<br><br><br><br><br><br>
<div class="dash">
<img src="image1.png">
<div id="slot1" class="slot ui-widget-header">
<p>holder</p>
</div>
<div id="slot2" class="slot ui-widget-header">
<p>holder</p>
</div>
<div id="slot3" class="slot ui-widget-header">
<p>holder</p>
</div>
<div id="slot4" class="slot ui-widget-header">
<p>holder</p>
</div>
<div id="slot5" class="slot ui-widget-header">
<p>holder</p>
</div>
<div id="slot6" class="slot ui-widget-header">
<p>holder</p>
</div>
<div id="slot7" class="slot ui-widget-header">
<p>holder</p>
</div>
<div id="slot8" class="slot ui-widget-header">
<p>holder</p>
</div>
</div>
</div>

关于javascript - 拖动图像下方的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56239331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com