图形碰撞

2017-04-20  本文已影响0人  洛洛kkkkkk
<!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>

上一篇 下一篇

猜你喜欢

热点阅读