| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- .box{
- width: 100px;
- height: 100px;
- background-color: red;
- position: fixed;
- top: 100px;
- left: 110px;
- }
- </style>
- </head>
- <body>
- <div class="box"></div>
- <script>
- var oBox = document.getElementsByClassName("box")[0];
- // 鼠标按下事件
- oBox.onmousedown = function(e){
- console.log("鼠标按下");
- console.log(e);
- // offsetLeft 当前元素距离窗口 左侧的间距
- console.log(oBox.offsetLeft);
- // offsetTop 获取当前元素距离窗口顶部的距离
- console.log(oBox.offsetTop);
-
- }
- // 鼠标弹起事件
- oBox.onmouseup = function(){
- console.log("鼠标弹起");
-
- }
- // 鼠标移动事件
- oBox.onmousemove = function(){
- console.log("鼠标移动");
-
- }
- </script>
- </body>
- </html>
|