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;

                             background: pink;

                             border-radius: 50%;

                             cursor: move;

                             position: absolute;

                     }

            </style>

    </head>

    <body>

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

    </body>

    <script>

            //获取元素

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

            console.log(ball);

            //声明全局变量

            var leftCha,topCha;

            //定义鼠标是否按下的标识

            var isDown = false;

            ball.onmousedown = function(e){

                     var e = e || window.event;

                     leftCha = e.clientX - ball.offsetLeft;

                     topCha = e.clientY - ball.offsetTop;

                     isDown = true;

            }

            window.onmousemove = function(e){

                     var e = e || window.event;

                     if(!isDown){

                             return;  //终止程序执行

                     }

                     ball.style.left = e.clientX - leftCha + 'px';

                     ball.style.top = e.clientY - topCha + 'px';

            }

            ball.onmouseup = function(e){

                     isDown = false;

            }

    </script>

    </html>

  • 相关阅读:
    c 中 static 关键字的作用
    关于声明变量关键字 extern 的搜索知识点
    思考在路上-虚拟机redhat系统安装tools
    一个小程序猿思考之路-头文件中#ifndef/#define/#endif作用和用法
    const 修饰的小看点(自己积点小知识)
    用css3实现闪烁效果
    icon font
    跟踪对象属性值的修改, 设置断点(Break on property change)
    setTimeout(fn, 0)引发的JavaScipt线程的思考
    "float: left;" div 不换行显示
  • 原文地址:https://www.cnblogs.com/sherryStudy/p/onmousedown.html
Copyright © 2011-2022 走看看