简体   繁体   English

通过HTML5拖放可拖动项的外观

[英]Appearance of draggable items with HTML5 drag and drop

My code follows: 我的代码如下:

<style>
   .box {
       border:1px solid black;
       margin:40px;
       padding:20px;
   }
   .dragitem {
       border:1px solid red;
       padding:10px;
       margin:10px;
   }
</style>

<div class="box" ondragover="allowDrop(event)" ondrop="drop(event)">

    <div class="dragitem" draggable="true" ondragstart="drag(event)">one</div>
    <div class="dragitem" draggable="true" ondragstart="drag(event)">two</div>
    <div class="dragitem" draggable="true" ondragstart="drag(event)">three</div>

</div>


<script>
 function drag(e) {
     e.dataTransfer.setData("Text",e.target.id);
 }
 function allowDrop(e) {
     e.preventDefault();
 }
 function drop(e){
     e.preventDefault();
     var data=e.dataTransfer.getData("Text");
     e.target.parentNode.appendChild(document.getElementById(data));
 }
</script>

When I drag one of the draggable divs, that div remains at its position and a semi-transparent clone is being moved. 当我拖动一个可拖动的div时,该div会保持在其位置,并且将移动一个半透明的克隆。 How may I achieve to remain nothing in its original position and when moving around to be visible exactly as it is? 我如何才能使任何东西都保持在其原始位置以及如何四处移动以使其完全可见? In other words, to move the element as it is exactly and without being cloned? 换句话说,要精确移动元素而不进行克隆?

One way to achieve this, would be to listen to the drag event ondrag and animate the element inside the listener to move along with the mouse. 实现此目的的一种方法是,侦听拖动事件ondrag并为侦听器中的元素设置动画,使其与鼠标一起移动。

This animation needs to be based on the initial x and y coordinates of the element and the distance and direction it's been dragged. 此动画需要基于元素的初始x和y坐标以及拖动元素的距离和方向。

You have to give an "id" to every element that you want drag/drop. 您必须为要拖放的每个元素赋予一个“ id”。

To give you an example I added the "id", a new div and I deleted the property ".parentNode" 举一个例子,我添加了“ id”,一个新的div并删除了属性“ .parentNode”

Then you can start to experiment 然后您可以开始尝试

 <style>
   .box {
       border:1px solid black;
       margin:40px;
       padding:20px;
       height: 200px;
   }
   .dragitem {
       border:1px solid red;
       padding:10px;
       margin:10px;
   }
</style>
<div id="div11" class="box" ondragover="allowDrop(event)" ondrop="drop(event)">

    <div id="div1" class="dragitem" draggable="true" ondragstart="drag(event)">one</div>
    <div id="div2" class="dragitem" draggable="true" ondragstart="drag(event)">two</div>
    <div id="div3" class="dragitem" draggable="true" ondragstart="drag(event)">three</div>

</div>

<div id="div12" class="box" ondragover="allowDrop(event)" ondrop="drop(event)">
</div>

<script>
 function drag(e) {
     e.dataTransfer.setData("Text",e.target.id);
 }
 function allowDrop(e) {
     e.preventDefault();
 }
 function drop(e){
     e.preventDefault();
     var data=e.dataTransfer.getData("Text");
     e.target.appendChild(document.getElementById(data));
 }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM