gpt4 book ai didi

javascript - 拖放无法正常工作

转载 作者:行者123 更新时间:2023-12-03 12:13:12 27 4
gpt4 key购买 nike

我是 JavaScript 新手。下面是我的用于拖放的 html。恢复无法正常工作。请帮助我为什么它不能正常工作。

在放下之前恢复正常,但无法返回到原始位置

        <!DOCTYPE html>
<html ng-app="first22">
<head>
<link rel="icon" type="image/png" href="globe/images/correct.png"/>
<link rel="stylesheet" type="text/css" href="globe/css/style.css"/>
<script type="text/javascript" src="globe/script/jquery.js"></script>
<script type="text/javascript" src="globe/script/jquery-ui.js"></script>
<title>
Html5 All in One
</title>
<style>
*{padding:0;margin:0}
#interactive
{
position:absolute;
width:895px;
height:695px;
margin:auto;
left:0;
right:0;
background:#f3f3f3;
}

.dragbg,.drop
{
position:absolute;
width:171px;
height:52px;
background:#c0c0c0;
font-size:20px;
border-radius:25px;
text-align:center;
}

.drag
{
width:160px;
height:40px;
background:#c2c2c2;
border:1px solid;
font-size:20px;
border-radius:25px;
text-align:center;
position:absolute;
margin-top:5px;
margin-left:5px;
line-height:40px;
cursor:pointer;
}

.drag:hover
{
background:#fff;
}
.drop1
{
width:160px;
height:40px;
background:#c2c2c2;
border:1px solid;
font-size:20px;
border-radius:25px;
text-align:center;
position:absolute;
top:5px;
left:5px;
line-height:40px;
cursor:pointer;
}
</style>
</head>
<body>
<div id="interactive">
<div style="position:absolute;left:0px;top:50px;width:100%;text-align:center;font-size:28px;font-weight:bold">Common Drag and Drop</div>

<div class="dragbg" style="left:120px;top:150px;">
<div class="drag" >Meter</div>
</div>
<div class="dragbg" style="left:120px;top:220px;">
<div class="drag">MilliMeter</div>
</div>
<div class="dragbg" style="left:120px;top:290px;">
<div class="drag">CentiMeter</div>
</div>
<div class="dragbg" style="left:120px;top:360px;">
<div class="drag">Gram</div>
</div>
<div class="dragbg" style="left:120px;top:430px;">
<div class="drag">MilliGram</div>
</div>
<div class="dragbg" style="left:120px;top:500px;">
<div class="drag">KiloGram</div>
</div>


<div class="drop" style="left:320px;top:150px;">

</div>
<div class="drop" style="left:320px;top:220px;">

</div>
<div class="drop" style="left:320px;top:290px;">

</div>
<div class="drop" style="left:320px;top:360px;">

</div>
<div class="drop" style="left:320px;top:430px;">

</div>
<div class="drop" style="left:320px;top:500px;">

</div>

</div>
</body>
<script>
$("document").ready(function()
{
$(".drag").draggable(
{
containment:"#interactive",
revert:function(event,ui)
{
$(this).data("uiDraggable").originalPosition=
{
left:0,
top:0
};
return !event;
},
zindex:1000,
drag:function(event,ui)
{
$(this).css("z-index",2000);
}
});

$(".drop").droppable(
{
drop:function(event,ui)
{
$(this).append(ui.draggable)
$(this).find(".drag").each(function()
{
$(this).css("position","absolute");
$(this).css({"top":"0px","left":"0px"});
});
}
});

})
</script>
</html>

最佳答案

您将拖动附加到其占位符,这会更改拖动对象的父对象。因此它改变了它与新容器的关系。您只需移动拖动的位置,将其保持在其父容器上。此外,您还必须相对于其容器定位 div。

            $(".drop").droppable(
{
drop:function(event,ui)
{
var pos = $(this).offset();
var ppos = $(ui.draggable).parent().offset();
var left = pos.left - ppos.left;
var top = pos.top - ppos.top;
$(ui.draggable).css({"left" : left, "top": top})
$(this).find(".drag").each(function()
{
$(this).css("position","absolute");
$(this).css({"top":"0px","left":"0px"});
});
}
});

你的 fiddle 已更新http://jsfiddle.net/XSXA6/18/

关于javascript - 拖放无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24858673/

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