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>

  • 相关阅读:
    封装Web Uploader 上传插件、My97DatePicker、百度 编辑器 的使用 (ASP.NET MVC)
    记一次 Newtonsoft.Json 巧妙的用法(C#)
    使用 ItextSharp HTML生成Pdf(C#)
    go 发布
    Winform 使用DotNetBar 根据菜单加载TabControl
    Winform 使用DotNetBar 设置界面为Office2007 样式
    DataTable 导出到TXT
    (Winform程序带源码) 弹出输入框和获取输入框的值
    C# 返回指定目录下所有文件信息
    Winform 应用DotnetBar
  • 原文地址:https://www.cnblogs.com/sherryStudy/p/onmousedown.html
Copyright © 2011-2022 走看看