如何使用 JavaScript 将图像拖放到现有图像之上?

亚当·布雷斯林

现在,我正在构建一个可以玩游戏的功能性棋盘,但是我在弄清楚如何让棋子拖放到一些绿点之上时遇到了麻烦。

问题图片: 在此处输入图片说明

我试图拖动这个棋子并将它放在两个点之一上,但我不知道如何去做。每当我尝试将棋子拖到绿点上方时,我的棋子最终都会消失。我该如何解决这个问题?

如果有帮助,绿点位于某些 div 标签内。绿点设置为不显示,仅在您单击典当时出现。我只需要人们看看第 5、6 和 7 行白棋子所在的位置和绿点所在的位置,就是这样。

我只为第 5 行和第 6 行的每一行中的一个 div 设置了一些拖动事件,我还为第 7 行的第一个白色 pawn 设置了一个拖动事件,我只是在其中的一个部分进行了测试我的板子,你可以从上图中看到。这是我的...

JsFiddle:https ://jsfiddle.net/nitroadam1233/ckn216px/2/

JavaScript 代码:

//dragging white pawns
function allowDrop(event) {
    event.preventDefault();
}

function drag(event) {
    event.dataTransfer.setData("text", event.target.id);
}
  
function drop(event) {
    event.preventDefault();
    var data = event.dataTransfer.getData("text");
    var newNode = document.getElementById(data).cloneNode(true);

    clearChildren(event.target);
    event.target.appendChild(newNode);
}

function clearChildren(el) {
    while (el.firstChild) {
      el.removeChild(el.firstChild);
    }  
}

HTML代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Title of the document</title>
        <link rel="stylesheet" href="style.css">
    </head>

    <body>
        <div id="board">
          
            <!-- 1st row -->
            <div><img src="images/black_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/black_knight.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/black_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/black_queen.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/black_king.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/black_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/black_knight.png"  draggable="true" ondragstart="drag(event)"height="65px" width="65px"></div>
            <div><img src="images/black_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
          
            <!-- 2nd row-->
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            <div><img src="images/black_pawn.png" draggable="true" ondragstart="drag(event)" class="black_pawns" height="65px" width="65px"></div>
            
            <!-- 3rd row empty-->
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
          
            <!-- 4th row empty-->
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
            <div class="empty_squares"></div>
          
            <!-- 5th row empty-->
            <div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
          
            <!-- 6th row empty-->
            <div class="empty_squares5" ondrop="drop(event)" ondragover="allowDrop(event)"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
            <div class="empty_squares5"><img src="images/greenDot.png" height="40px" width="60px"></div>
          
            <!-- 7th row-->
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="item1" ondragstart="drag(event)" onmousedown="dontShowDots(event)" onclick="showDots()" height="65px" width="65px"></div>
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="" class="" onmousedown="dontShowDots2(event)" onclick="showDots2()" height="65px" width="65px"></div>
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="" class="" onmousedown="dontSD3(event)" onclick="showDots3()" height="65px" width="65px"></div>
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="" class="" onmousedown="dontShowDots4(event)" onclick="showDots4()" height="65px" width="65px"></div>
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="" class="" onmousedown="dontShowDots5(event)" onclick="showDots5()" height="65px" width="65px"></div>
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="" class="" onmousedown="dontShowDots6(event)" onclick="showDots6()" height="65px" width="65px"></div>
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="" class="" onmousedown="dontShowDots7(event)" onclick="showDots7()" height="65px" width="65px"></div>
            <div class="seventh_squares"><img src="images/white_pawn.png" draggable="true" id="" class="" onmousedown="dontShowDots8(event)" onclick="showDots8()" height="65px" width="65px"></div>
          
            <!-- 8th row-->
            <div><img src="images/white_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/white_knight.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/white_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/white_queen.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/white king.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/white_bishop.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/white_knight.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>
            <div><img src="images/white_rook.png" draggable="true" ondragstart="drag(event)" height="65px" width="65px"></div>

            <script src="interactive.js"></script>
        </div>
    </body>
</html>

代码有很多可以优化的地方,但是要解决具体问题,需要对 drop 函数进行如下修改,注释表明这两个新行后面的目的......

function drop(event) {
    event.preventDefault();
    var data = event.dataTransfer.getData("text");
    var newNode = document.getElementById(data).cloneNode(true);

    // Clear the drag square of the piece IMG...
    clearChildren( document.getElementById(data).parentNode );
    
    clearChildren(event.target);
    event.target.appendChild(newNode);
    
    // Finally, remove the "empty_squares5" class from the drop square.
    event.target.classList.remove( 'empty_squares5' );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用JavaScript检测图像是否已粘贴或拖放到可编辑的区域?

如何使用JavaScript将图像添加到页面中的现有表中

使用普通JavaScript拖放图像效果不佳

仅当元素和div在javascript中具有相同的ID时,才如何将元素拖放到div中?

使用 Angular6 将图像拖放到 div 中并显示预览

如何使用 JavaScript 将图像链接/url 转换为图像

如何使用JavaScript放大图像?

将fabric.js和jQuery结合使用时将图像拖放到画布上不起作用

如何使用 vanilla javascript 在 HTML 画布上拖放和预览图像文件?

如何使用JavaScript将边框更改为上传的图像

如何使用JavaScript将幻灯片图像居中?

如何使用Smokescreen / JavaScript将Flash转换为图像

如何使用javascript将图像标签设置为html表?

如何使用Javascript将FontAwesome渲染为SVG图像?

使用javascript时,如何设置将更改图像的<div>?

如何使用javascript将图像从PNG转换为JPEG?

如何使用javascript将图像插入到div中?

使用JavaScript单击图像A时,如何将图像B的src更改为图像A的src?

如何将图像放在引导程序中的图像之上;

使用 JavaScript 和 asp.net core 从数据库中删除现有图像

使用 JavaScript 随机化一组现有图像的布局

使用纯JavaScript将十字符号拖放到网格中的框中?

使用javascript将具有多个图像的html div转换为单个图像

如何使用javascript覆盖现有对象

如何使用javascript预加载图像

如何使用JavaScript在HTML中显示图像

如何使用javascript获取图像alt标签?

如何使用javascript让图像上下移动

如何使用Javascript从图像转换为文本