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>

  • 相关阅读:
    vim的分屏功能
    vim进阶
    VIM常用快捷键
    vim操作:打开多个文件、同时显示多个文件、在文件之间切换
    vim 如何复制文件中多行到另一个文件
    无限分类左右值算法的常规操作逻辑
    js查看Object对象的内容
    js获取当前页面的url信息
    javascript获取url中的参数值
    解决Eclipe安装不上android的ADT的办法
  • 原文地址:https://www.cnblogs.com/sherryStudy/p/onmousedown.html
Copyright © 2011-2022 走看看