元素拖拽小游戏

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bigBox {
            width: 300px;
            height: 300px;
            position: relative;
            margin: 100px auto;
            font-size: 20px;
            font-weight: 600;
            line-height: 100px;
            text-align: center;
            color: #fff;
        }

        .bigBox>div {
            border: 1px solid #000;
            /* 禁止选中 */
            user-select: none;
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
        }

        .one {
            width: 100px;
            height: 100px;
            background: #0ba7ce;
            cursor: pointer;
            position: absolute;
            left: 0;
            top: 0;
        }

        .two {
            width: 100px;
            height: 100px;
            background: #afdd31;
            cursor: pointer;
            position: absolute;
            left: 100px;
            top: 0;
        }

        .three {
            width: 100px;
            height: 100px;
            background: #5ace37;
            cursor: pointer;
            position: absolute;
            left: 200px;
            top: 0;
        }

        .four {
            width: 100px;
            height: 100px;
            background: #2feee4;
            cursor: pointer;
            position: absolute;
            left: 0;
            top: 100px;
        }

        .five {
            width: 100px;
            height: 100px;
            background: #727de2;
            cursor: pointer;
            position: absolute;
            left: 100px;
            top: 100px;
        }

        .six {
            width: 100px;
            height: 100px;
            background: #e4a16a;
            cursor: pointer;
            position: absolute;
            left: 200px;
            top: 100px;
        }

        .seven {
            width: 100px;
            height: 100px;
            background: #c5286a;
            cursor: pointer;
            position: absolute;
            left: 0;
            top: 200px;
        }

        .eight {
            width: 100px;
            height: 100px;
            background: #05b45d;
            cursor: pointer;
            position: absolute;
            left: 100px;
            top: 200px;
        }

        .night {
            width: 100px;
            height: 100px;
            background: #7a481f;
            cursor: pointer;
            position: absolute;
            left: 200px;
            top: 200px;
        }
    </style>
</head>

<body>
    <div class="bigBox">
        <div class="one" id="one">1</div>
        <div class="two" id="two">2</div>
        <div class="three" id="three">3</div>
        <div class="four" id="four">4</div>
        <div class="five" id="five">5</div>
        <div class="six" id="six">6</div>
        <div class="seven" id="seven">7</div>
        <div class="eight" id="eight">8</div>
        <div class="night" id="night">9</div>
    </div>

    <script>
        document.title = '元素拖拽'

        // 获取元素
        let one = document.getElementById('one');
        let two = document.getElementById('two');
        let three = document.getElementById('three');
        let four = document.getElementById('four');
        let five = document.getElementById('five');
        let six = document.getElementById('six');
        let seven = document.getElementById('seven');
        let eight = document.getElementById('eight');
        let night = document.getElementById('night');
        // 将元素放入数组中
        let arr = [one, two, three, four, five, six, seven, eight, night]
        let idx = 1
        // 拖拽函数
        function boxDrag(ele) {
            let x = 0;
            let y = 0;
            ele.onmousedown = (e) => {
                let disX = e.clientX - ele.offsetLeft;
                let disY = e.clientY - ele.offsetTop;
                x = e.clientX - disX;
                y = e.clientY - disY;

                // 鼠标按下时,元素置顶
                let timer = setInterval(() => {
                    idx += 10
                    ele.style.zIndex = 1000 + idx
                }, 100)

                document.onmousemove = (e) => {
                    // 鼠标移动时,元素跟随鼠标移动
                    let nowx = e.clientX - disX;
                    let nowy = e.clientY - disY;
                    ele.style.left = nowx + 'px';
                    ele.style.top = nowy + 'px';
                }
                document.onmouseup = (e) => {
                    // 储存元素
                    let newArr = []
                    // 碰撞坐标互换
                    arr.forEach((item, index) => {
                        if (item !== ele) {
                            // 碰撞面积检测
                            if (boxCollision(ele, item)) {
                                newArr.push({
                                    index,
                                    area: boxCollision(ele, item),
                                    left: item.offsetLeft,
                                    top: item.offsetTop,
                                })

                            }
                        }
                    })
                    if (newArr.length) {
                        // 碰撞面积排序
                        newArr.sort((a, b) => {
                            return b.area - a.area;
                        })
                        // 碰撞面积最大的元素
                        let maxArea = newArr[0];
                        let maxAreaIndex = maxArea.index;
                        ele.style.left = maxArea.left + 'px';
                        ele.style.top = maxArea.top + 'px';
                        // 碰撞面积最大的元素回归原位
                        arr[maxAreaIndex].style.left = x + 'px';
                        arr[maxAreaIndex].style.top = y + 'px';
                    } else {
                        // 鼠标抬起时,元素回归原位
                        ele.style.left = x + 'px';
                        ele.style.top = y + 'px';
                    }
                    clearInterval(timer);
                    document.onmousemove = null;
                    document.onmouseup = null;
                }
            }
        }
        boxDrag(one);
        boxDrag(two);
        boxDrag(three);
        boxDrag(four);
        boxDrag(five);
        boxDrag(six);
        boxDrag(seven);
        boxDrag(eight);
        boxDrag(night);


        // 碰撞检测
        function boxCollision(ele1, ele2) {
            let x1 = ele1.offsetLeft;
            let y1 = ele1.offsetTop;
            let x2 = ele2.offsetLeft;
            let y2 = ele2.offsetTop;
            let w1 = ele1.offsetWidth;
            let h1 = ele1.offsetHeight;
            let w2 = ele2.offsetWidth;
            let h2 = ele2.offsetHeight;
            if (x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1) {
                return false;
            } else {
                // 计算碰撞面积
                let area1 = (x1 + w1 < x2 + w2 ? x1 + w1 : x2 + w2) - (x1 > x2 ? x1 : x2);
                let area2 = (y1 + h1 < y2 + h2 ? y1 + h1 : y2 + h2) - (y1 > y2 ? y1 : y2);
                let area = area1 * area2;

                return area;
            }
        }

    </script>
</body>

</html>

要制作一个拖拽小游戏,可以使用HTML5中的拖放API。首先需要在HTML中创建可拖动元素和目标元素,然后使用JavaScript代码来实现拖放功能。 以下是一个简单的示例代码,展示了如何制作一个拖拽小游戏: ```html <!DOCTYPE html> <html> <head> <title>拖拽小游戏</title> <style> #drag { width: 100px; height: 100px; background-color: red; cursor: move; } #drop { width: 200px; height: 200px; background-color: blue; } </style> </head> <body> <div id="drag" draggable="true"></div> <div id="drop"></div> <script> var drag = document.getElementById("drag"); var drop = document.getElementById("drop"); drag.addEventListener("dragstart", function(event) { event.dataTransfer.setData("text/plain", event.target.id); }); drop.addEventListener("dragover", function(event) { event.preventDefault(); }); drop.addEventListener("drop", function(event) { event.preventDefault(); var data = event.dataTransfer.getData("text/plain"); var element = document.getElementById(data); event.target.appendChild(element); }); </script> </body> </html> ``` 在这个示例中,我们创建了一个可拖动的红色方块,并将其拖放到了一个蓝色的方框中。我们使用了拖放API中的dragstart、dragover和drop事件来实现这个功能。当用户开始拖动可拖动元素时,会触发dragstart事件,我们在该事件的处理程序中使用setData方法将被拖动元素的ID存储为文本数据。当用户将被拖动元素拖到目标元素上时,会触发drop事件,我们在该事件的处理程序中使用getData方法获取被拖动元素的ID,并将其附加到目标元素上。同时,为了防止浏览器默认的drop事件,我们也需要在目标元素上处理dragover事件并调用event.preventDefault()方法。 当然,这只是一个简单的示例,如果你想制作更复杂的拖拽小游戏,你需要更多的JavaScript编程知识和技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值