• 【案例】DIV随鼠标移动而移动


    <!DOCTYPE html>

    <html lang="en">

    <head>

            <meta charset="UTF-8">

            <title>div跟随鼠标移动而移动</title>

            <style>

                     *{

                             margin: 0;

                             padding: 0;

                     }

                     #ball{

                             200px;

                             height: 200px;

                             border-radius: 50%;

                             background: pink;

                             position: absolute;

                             cursor: move;

                     }

            </style>

    </head>

    <body>

            <div id="ball"></div>

    </body>

    <script>

            //获取元素

            var ball = document.getElementById('ball');

            console.log(ball);

            //将鼠标的移动事件交给外部更大的容器window,以保证鼠标不丢失

            /*ball.onmousemove = function(e){}*/

            window.onmousemove = function(e){

                     var e = e || window.event;

                     var newLeft = e.clientX - ball.offsetWidth / 2;

                     var newTop = e.clientY - ball.offsetHeight / 2;

                     ball.style.left = newLeft + 'px';

                     ball.style.top = newTop + 'px';

            }

    </script>

    </html>

  • 相关阅读:
    UVA 10905
    UVA 10859 树形DP
    LA 4794 状态DP+子集枚举
    LA 3695 部分枚举
    UVA 11825 状态压缩DP+子集思想
    UVA 10891 区间DP+博弈思想
    HDU 5239 上海大都会 D题(线段树+数论)
    HDU 5242 上海大都会 G题
    HDU 5241 上海大都会 F题
    P1359 租用游艇
  • 原文地址:https://www.cnblogs.com/sherryStudy/p/mousemove.html
Copyright © 2020-2023  润新知