javascript中的可拖动元素

时间:2016-05-29 15:33:40

标签: javascript html css drag-and-drop draggable

如果我想使div可拖动,我必须指定一个引用父类?例如在jquery中,当一个元素是可拖动的时它是否有引用父元素?

1 个答案:

答案 0 :(得分:1)

要拖动元素,只需添加draggable属性并将其值设置为true

这不适用于Internet Explorer 8或更早版本等古老浏览器,因为它们也不支持draggable属性。

您可能正在考虑的是放弃目标。默认情况下,元素不能放在其他元素中。

请注意,以下拖动事件需要event参数。

  • ondrag :每当拖动元素时都会触发事件

  • ondragstart - 启动拖动过程时会触发事件

  • ondragend - 拖动过程完成后会触发事件

  • ondragenter - 当被拖动的元素进入放置目标时会触发事件

  • ondragleave - 当被拖动的元素离开放置目标时会触发事件

  • ondragover - 当拖动的元素位于放置目标上时会触发事件

要允许删除,我们必须通过event.preventDefault更改浏览器的默认行为。

这个例子是由W3Schools提供的。但是,它解释了拖放功能。不幸的是,拖放事件没有pseudo classes因此我们需要使用本机JavaScript。

http://www.w3schools.com/jsref/event_ondragstart.asp

var demo = document.getElementById("demo");

// Use dataTransfer.setData() to set data type and value of dragged data
// When dragging starts, the paragraph will output text and opacity will decrease
document.addEventListener("dragstart", function(event) {
    event.dataTransfer.setData("Text", event.target.id);
    demo.innerHTML = "Started to drag the p element.";
    event.target.style.opacity = "0.4";
});

document.addEventListener("drag", function(event) {
    demo.style.color = "red";                    // Change color of output text
});

// Output some text when finished dragging the p element and reset the opacity
document.addEventListener("dragend", function(event) {
    demo.innerHTML = "Finished dragging the p element.";
    event.target.style.opacity = "1";
});


/* Events fired on the drop target */

// When the draggable p element enters the droptarget, change the DIVS's border style
document.addEventListener("dragenter", function(event) {
    if ( event.target.className == "droptarget" ) {
        event.target.style.border = "3px dotted red";
    }
});

// By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element
document.addEventListener("dragover", function(event) {
    event.preventDefault();
});

// When the draggable p element leaves the droptarget, reset the DIVS's border style
document.addEventListener("dragleave", function(event) {
    if ( event.target.className == "droptarget" ) {
        event.target.style.border = "";
    }
});

/* On drop - Prevent the browser default handling of the data (default is open as link on drop)
   Reset the color of the output text and DIV's border color
   Get the dragged data with the dataTransfer.getData() method
   The dragged data is the id of the dragged element ("drag1")
   Append the dragged element into the drop element
*/
document.addEventListener("drop", function(event) {
    event.preventDefault();
    if ( event.target.className == "droptarget" ) {
        demo.style.color = "";
        event.target.style.border = "";
        var data = event.dataTransfer.getData("Text");
        event.target.appendChild(document.getElementById(data));
    }
});
.droptarget {
    float: left; 
    width: 100px; 
    height: 35px;
    margin: 15px;
    padding: 10px;
    border: 1px solid #aaaaaa;
}
<p>Drag the p element back and forth between the two rectangles:</p>

<div class="droptarget">
  <p draggable="true" id="dragtarget">Drag me!</p>
</div>

<div class="droptarget"></div>

<p style="clear:both;"><strong>Note:</strong> drag events are not supported in Internet Explorer 8 and earlier versions or Safari 5.1 and earlier versions.</p>

<p id="demo"></p>