美文网首页
图形碰撞

图形碰撞

作者: 洛洛kkkkkk | 来源:发表于2017-04-20 19:17 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>检测圆形碰撞</title>
            <style type="text/css">
                div{
                    border-radius: 50%;
                }
                .redDiv{
                    width: 200px;
                    height: 200px;
                    background-color: red;
                }
                .blue{
                    width: 100px;
                    height: 100px;
                    background-color: blue;
                    position: absolute;
                }
                
            </style>
        </head>
        <body>
            <div class="redDiv"></div>
            <div class="blue"></div>
        </body>
        <script type="text/javascript">
            //让蓝色Div可以拖拽移动
            var redDiv=document.querySelector(".redDiv");
            var blueDiv=document.querySelector(".blue");
            blueDiv.onmousedown= function (ev) {
                    var x=ev.offsetX;
                    var y=ev.offsetY;
                    document.onmousemove= function (ev) {
                        blueDiv.style.left=ev.clientX-x+"px";
                        blueDiv.style.top=ev.clientY-y+"px";
                    }
            }
            blueDiv.onmouseup =function () {
                document.onmousemove =null;
            }
        </script>
    </html>
    
    

    相关文章

      网友评论

          本文标题:图形碰撞

          本文链接:https://www.haomeiwen.com/subject/ryolzttx.html