17_按下弹起事件.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box{
  9. width: 100px;
  10. height: 100px;
  11. background-color: red;
  12. position: fixed;
  13. top: 100px;
  14. left: 110px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="box"></div>
  20. <script>
  21. var oBox = document.getElementsByClassName("box")[0];
  22. // 鼠标按下事件
  23. oBox.onmousedown = function(e){
  24. console.log("鼠标按下");
  25. console.log(e);
  26. // offsetLeft 当前元素距离窗口 左侧的间距
  27. console.log(oBox.offsetLeft);
  28. // offsetTop 获取当前元素距离窗口顶部的距离
  29. console.log(oBox.offsetTop);
  30. }
  31. // 鼠标弹起事件
  32. oBox.onmouseup = function(){
  33. console.log("鼠标弹起");
  34. }
  35. // 鼠标移动事件
  36. oBox.onmousemove = function(){
  37. console.log("鼠标移动");
  38. }
  39. </script>
  40. </body>
  41. </html>