【JS功能】JS实现一个九宫格拖拽交换小游戏

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

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>九宫格拖拽交换</title>
    <style>
        #box {
            width: 303px;
            height: 303px;
            margin: 100px auto;
        }

        #box div {
            width: 100px;
            height: 100px;
            float: left;
            margin-left: 1px;
            margin-top: 1px;
            text-align: center;
            line-height: 100px;
            font-size: 50px;
            color: #FFF;

        }

        #box div:nth-of-type(1) {
            background: #000;
        }

        #box div:nth-of-type(2) {
            background: #f00;
        }

        #box div:nth-of-type(3) {
            background: #0f0;
        }

        #box div:nth-of-type(4) {
            background: #00f;
        }

        #box div:nth-of-type(5) {
            background: #ff0;
        }

        #box div:nth-of-type(6) {
            background: #f0f;
        }

        #box div:nth-of-type(7) {
            background: #0ff;
        }

        #box div:nth-of-type(8) {
            background: pink;
        }

        #box div:nth-of-type(9) {
            background: orange;
        }

        #box .draging {
            position: absolute;

        }
    </style>
</head>

<body>
    <div id="box">
        <div>1</div>
        <div style="z-index: 1;">2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
    </div>

    <script>
        /*

        为API而生,为框架死,为debug而奋斗一辈子,
        吃了符号的亏,上了大小写的当,最后死在了需求上
        */

        /* 
            给 所有的div添加点击id事件,点击的时候 创建一个div 跟点击的div 一样
            并且让点击idiv 可以拖拽 ,  
            
            松手的 找出 div 距离 9个div 哪个最近,

            让div 的内容和颜色 给 最近的交换,  最后新 div删除
        */
        let oDivs = document.querySelectorAll('#box div')
        let box = document.querySelector("#box")
        oDivs.forEach(function (div, i) {
            div.onmousedown = function (e) {
                let offsetX = e.offsetX;
                let offsetY = e.offsetY;
                console.log(this, div);

                // var newDiv = document.createElement("div")
                var newDiv = div.cloneNode(true) //参数为true 复制子节点,false 不复制子节点



                // newDiv.innerHTML = div.innerHTML;
                newDiv.style.background = getComputedStyle(div).background;
                newDiv.classList.add('draging')

                box.appendChild(newDiv)

                newDiv.style.left = e.pageX - offsetX + 'px';
                newDiv.style.top = e.pageY - offsetY + 'px';


                document.onmousemove = function (evt) {
                    newDiv.style.left = evt.pageX - offsetX + 'px';
                    newDiv.style.top = evt.pageY - offsetY + 'px';
                }

                document.onmouseup = function () {
                    //停止拖拽 
                    document.onmousemove = null

                    // 松手的 找出 div 距离 9个div 哪个最近,
                    let arr = []
                    oDivs.forEach(function (oDiv) {
                        //oDiv 和 newDiv的距离
                        let d = distance(oDiv, newDiv);
                        arr.push(d)
                    })
                    console.log(arr);
                    let min = Math.min(...arr);
                    let minIndex = arr.indexOf(min)
                    let minDiv = oDivs[minIndex]
                    console.log(minDiv);

                    // 让div 的内容
                    // 和颜色 给 最近的交换,  最后新 div删除

                    // minDiv  oDiv
                    let tempColor = getComputedStyle(minDiv).backgroundColor
                    let tempText = minDiv.innerHTML;

                    minDiv.style.backgroundColor = getComputedStyle(div).backgroundColor
                    minDiv.innerHTML = div.innerHTML;

                    div.innerHTML = tempText;
                    div.style.backgroundColor = tempColor

                    newDiv.remove()









                }




            }
        })






        function distance(div1, div2) {
            //勾股定理 记录 两个元素的距离
            var a = div1.offsetLeft - div2.offsetLeft;
            var b = div1.offsetTop - div2.offsetTop;
            var c = Math.sqrt(a * a + b * b);
            return c;
        }
    </script>

</body>

</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以,以下是一个简单的九宫格拖拽实现代码: HTML: ``` <div id="container"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> ``` CSS: ``` #container { width: 300px; height: 300px; border: 1px solid #000; display: flex; flex-wrap: wrap; } .box { width: 100px; height: 100px; background-color: #ccc; border: 1px solid #000; box-sizing: border-box; cursor: move; } ``` JavaScript: ``` var container = document.getElementById('container'); var boxes = document.getElementsByClassName('box'); var activeBox = null; var activeBoxIndex = null; var startX = null; var startY = null; for (var i = 0; i < boxes.length; i++) { boxes[i].addEventListener('mousedown', function(e) { activeBox = e.target; activeBoxIndex = Array.prototype.indexOf.call(boxes, activeBox); startX = e.clientX; startY = e.clientY; }); boxes[i].addEventListener('mousemove', function(e) { if (activeBox) { var deltaX = e.clientX - startX; var deltaY = e.clientY - startY; activeBox.style.transform = 'translate(' + deltaX + 'px, ' + deltaY + 'px)'; } }); boxes[i].addEventListener('mouseup', function(e) { if (activeBox) { var endX = e.clientX; var endY = e.clientY; var endBoxIndex = Math.floor(endY / 100) * 3 + Math.floor(endX / 100); if (endBoxIndex !== activeBoxIndex) { var endBox = boxes[endBoxIndex]; container.insertBefore(activeBox, endBox); } activeBox.style.transform = 'translate(0, 0)'; activeBox = null; activeBoxIndex = null; } }); } ``` 这段代码实现一个简单的九宫格拖拽效果,你可以在 HTML 中添加更多的 box 元素来增加九宫格的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺旺大力包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值