gpt4 book ai didi

javascript - 将家具拖放到房间中

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:58 26 4
gpt4 key购买 nike

我得到了这个房间,还有一些家具。
1.我希望在放下家具时,家具在地板上方而不是地板下方。
2.我想交换家具,所以当你把家具放到地板上时,已经有家具的地方应该交换位置。

This is what I have made for now...

我的代码:

$(document).ready(function() {
$('#div2').on("drop", function(e) {
e.preventDefault();
e.stopPropagation();
});
});

function allowDrop(ev) {
ev.preventDefault();
}

function drag(ev) {
ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
#floor {
top:116px;
left:393px;
position:absolute;
margin-bottom: 10px;
height: 40px;
width: 65px;
background-image: url("http://i.imgur.com/tCuykFV.png")
}

#floor:hover {
height: 43px;
width: 66px;
background-image: url("http://i.imgur.com/Eo1dNNv.png")
}

#space {
width:200px;
}

#div3 {
float: right;
border: 1px solid #CCC;
margin-bottom: 10px;
height: 200px;
width: 102px;
}

#dice {
width:56px;
height:79px;
}

#walls {
position:absolute;
top:0px;
left:0px;
width:688px;
height:510px;
border: 1px solid #CCC;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="js.js"></script>
</head>

<img id="walls" src="http://i.imgur.com/FA6ka0v.png">

<div id="floor" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="floor" ondrop="drop(event)" ondragover="allowDrop(event)" style="top:132px;left:424px;"></div>

<div id="floor" ondrop="drop(event)" ondragover="allowDrop(event)" style="top:132px;left:360px;"></div>



<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)">

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
<img src="https://hydra-media.cursecdn.com/habbo.gamepedia.com/e/ed/Edicehc.png?version=6e01ba71341b8361df23749c65498f44" draggable="true" ondragstart="drag(event)" id="drag1" width="56" height="79">

<img src="https://hydra-media.cursecdn.com/habbo.gamepedia.com/0/0f/Mocchamaster.png?version=6cf4d970f845287fa21d4ef7691eee84" draggable="true" ondragstart="drag(event)" id="drag2" width="66" height="137">

</div>

</div>

最佳答案

为拖动的图片添加下面的样式

#floor img {
position: absolute;
bottom: 0;}

检查下面的代码:

$(document).ready(function() {
$('#div2').on("drop", function(e) {
e.preventDefault();
e.stopPropagation();
});
});

function allowDrop(ev) {
ev.preventDefault();
}

function drag(ev) {
ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("Text");
var temp = null;
if(ev.target.nodeName == 'IMG'){
temp = $(ev.target);
$(ev.target).parent().empty().append(document.getElementById(data));
$('#div3 #div1').append(temp);
}
else
ev.target.appendChild(document.getElementById(data));
}
#floor {
top:116px;
left:393px;
position:absolute;
margin-bottom: 10px;
height: 40px;
width: 65px;
background-image: url("http://i.imgur.com/tCuykFV.png")
}

#floor:hover {
height: 43px;
width: 66px;
background-image: url("http://i.imgur.com/Eo1dNNv.png")
}

#space {
width:200px;
}

#div3 {
float: right;
border: 1px solid #CCC;
margin-bottom: 10px;
height: 200px;
width: 102px;
}

#dice {
width:56px;
height:79px;
}

#walls {
position:absolute;
top:0px;
left:0px;
width:688px;
height:510px;
border: 1px solid #CCC;
}
#floor img {
position: absolute;
bottom: 0;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="js.js"></script>
</head>

<img id="walls" src="http://i.imgur.com/FA6ka0v.png">

<div id="floor" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="floor" ondrop="drop(event)" ondragover="allowDrop(event)" style="top:132px;left:424px;"></div>

<div id="floor" ondrop="drop(event)" ondragover="allowDrop(event)" style="top:132px;left:360px;"></div>



<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)">

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
<img src="https://hydra-media.cursecdn.com/habbo.gamepedia.com/e/ed/Edicehc.png?version=6e01ba71341b8361df23749c65498f44" draggable="true" ondragstart="drag(event)" id="drag1" width="56" height="79">

<img src="https://hydra-media.cursecdn.com/habbo.gamepedia.com/0/0f/Mocchamaster.png?version=6cf4d970f845287fa21d4ef7691eee84" draggable="true" ondragstart="drag(event)" id="drag2" width="66" height="137">

</div>

</div>

关于javascript - 将家具拖放到房间中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43677588/

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