https://github.com/zxuqian/html-css-examples/blob/master/34-drag-drop-api/index.html

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <style>
    8. * {
    9. box-sizing: border-box;
    10. padding: 0;
    11. margin: 0;
    12. }
    13. main {
    14. width: 100vw;
    15. height: 100vh;
    16. display: grid;
    17. grid-template-columns: 1fr 1fr;
    18. place-items: center;
    19. background-color: hsl(0deg, 0%, 10%);
    20. }
    21. .draggable-container {
    22. width: 100%;
    23. height: 100%;
    24. display: grid;
    25. place-items: center;
    26. }
    27. .draggable,
    28. .droppable {
    29. border-radius: 4px;
    30. }
    31. .draggable {
    32. width: 25vw;
    33. height: 25vw;
    34. background: #00d9ff;
    35. }
    36. .droppable {
    37. width: 30vw;
    38. height: 30vw;
    39. border: 8px dashed #00d9ff;
    40. position: relative;
    41. display: grid;
    42. place-items: center;
    43. }
    44. .droppable::before {
    45. display: block;
    46. content: "请拖放到此区域";
    47. position: absolute;
    48. color: white;
    49. font-family: sans-serif;
    50. font-size: 3vw;
    51. color: hsl(0, 0%, 30%);
    52. }
    53. .droppable img {
    54. width: 80%;
    55. height: 80%;
    56. object-fit: contain;
    57. }
    58. .dragover {
    59. border: 8px dashed #ffae00;
    60. }
    61. .dropped {
    62. border: 8px dashed #48ff00;
    63. }
    64. .dropped::before {
    65. z-index: -1;
    66. }
    67. </style>
    68. <title>原生拖拽</title>
    69. </head>
    70. <body>
    71. <main>
    72. <div class="draggable-container">
    73. <div id="draggable" class="draggable" draggable="true"></div>
    74. </div>
    75. <div id="droppable" class="droppable"></div>
    76. </main>
    77. <script>
    78. const draggable = document.getElementById("draggable");
    79. const droppable = document.getElementById("droppable");
    80. draggable.addEventListener("dragstart", handleDragStart);
    81. droppable.addEventListener("dragover", handleDragover);
    82. droppable.addEventListener("dragleave", handleDragLeave);
    83. droppable.addEventListener("drop", handleDrop);
    84. function handleDragStart(e) {
    85. console.log('222, ', e.dataTransfer.setData)
    86. e.dataTransfer.setData("text/plain", e.target.id);
    87. // 有 copy、move、和 auto 三种,分别是把鼠标指针
    88. // 显示为复制样式(带+号)、移动样式,或自动设置样式
    89. // e.dataTransfer.dropEffect = "move";
    90. }
    91. function handleDragover(e) {
    92. console.log('111-11', e)
    93. e.preventDefault();
    94. droppable.classList.add("dragover");
    95. // e.dataTransfer.dropEffect = "move";
    96. }
    97. function handleDragLeave(e) {
    98. console.log('222-222', e)
    99. droppable.classList.remove("dragover");
    100. }
    101. function handleDrop(e) {
    102. console.log('333-33', e)
    103. e.preventDefault();
    104. // 如果是文件
    105. [...e.dataTransfer.items].forEach((item) => {
    106. if (item.kind === "file") {
    107. const file = item.getAsFile();
    108. createPreview(file);
    109. }
    110. });
    111. // 如果是元素
    112. const draggedId = e.dataTransfer.getData("text/plain");
    113. droppable.appendChild(document.getElementById(draggedId));
    114. droppable.classList.add("dropped");
    115. }
    116. function createPreview(imageFile) {
    117. if (!imageFile.type.startsWith("image/")) {
    118. return;
    119. }
    120. const image = document.createElement("img");
    121. image.src = URL.createObjectURL(imageFile);
    122. image.onload = function () {
    123. URL.revokeObjectURL(this.src);
    124. };
    125. droppable.appendChild(image);
    126. }
    127. </script>
    128. </body>
    129. </html>