zoukankan      html  css  js  c++  java
  • 【案例】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>

  • 相关阅读:
    UML之对象图
    android5.0(Lollipop) BLE Peripheral牛刀小试
    自己定义msi安装包的运行过程
    高速排序
    字符串函数---strcat()与strncat具体解释及实现
    杭电(hdu)1181 变形课
    电脑突然死机,编译报错dll缺少依赖项
    谈谈java垃圾回收机制
    swift(2)元祖(Tuple)
    IOS小工具以及精彩的博客
  • 原文地址:https://www.cnblogs.com/sherryStudy/p/mousemove.html
Copyright © 2011-2022 走看看