10.事件相关的练习-拖拽四.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #box1 {
  10. width: 100px;
  11. height: 100px;
  12. background-color: red;
  13. position: absolute;
  14. }
  15. #box2 {
  16. width: 100px;
  17. height: 100px;
  18. background-color: yellow;
  19. position: absolute;
  20. left: 200px;
  21. top: 200px;
  22. }
  23. </style>
  24. <script>
  25. window.onload = function () {
  26. /**
  27. * 拖拽box1元素
  28. * -拖拽的流程
  29. * 1.当鼠标在被拖拽元素上按下时,开始拖拽 onmousedown
  30. * 2.当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
  31. * 3.当鼠标松开时,被拖拽元素固定在当前位置 onmouseup
  32. */
  33. //获取box1和box2
  34. var box1 = document.getElementById("box1");
  35. var box2 = document.getElementById("box2");
  36. var img1 = document.getElementById("img1");
  37. //开启box1的拖拽
  38. drag(box1);
  39. //开启box2的拖拽
  40. drag(box2);
  41. drag(img1);
  42. };
  43. /**
  44. * 提取一个专门用来设置拖拽的函数
  45. * 参数:
  46. */
  47. function drag(obj){
  48. //为box1绑定一个鼠标按下事件
  49. obj.onmousedown = function (event) {
  50. //设置box1捕获所有鼠标按下的事件
  51. /**
  52. * setCapture()
  53. * -只有IE支持,但是在火狐中调用时不会报错,
  54. * 而如果使用Chrome调用,会报错
  55. */
  56. // if (box1.setCapture) {
  57. // box1.setCapture();
  58. // };
  59. //也可以写成下面的格式,更简洁
  60. obj.setCapture && obj.setCapture();
  61. // box1.setCapture();//Edge不可用
  62. event = event || window.event;
  63. //div的偏移量 鼠标.clientX - 元素.offsetLeft
  64. //div的偏移量 鼠标.clientY - 元素.offsetTop
  65. var ol = event.clientX - obj.offsetLeft;
  66. var ot = event.clientY - obj.offsetTop;
  67. //为document绑定一个onmousemove事件
  68. document.onmousemove = function (event) {
  69. event = event || window.event;
  70. //当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
  71. //获取鼠标的坐标
  72. var left = event.clientX - ol;
  73. var top = event.clientY - ot;
  74. //修改box1的位置
  75. obj.style.left = left + "px";
  76. obj.style.top = top + "px";
  77. };
  78. //为元素绑定一个鼠标松开事件
  79. document.onmouseup = function () {
  80. //当鼠标松开时,被拖拽元素固定在当前位置 onmouseup
  81. //取消document的onmousemove事件
  82. document.onmousemove = null;
  83. //取消document的onmouseup事件
  84. document.onmouseup = null;
  85. // alert("鼠标松开了");
  86. //当鼠标松开时,取消对事件的捕获
  87. obj.releaseCapture && obj.releaseCapture();
  88. };
  89. /**
  90. * 当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,
  91. * 此时会导致拖拽功能的异常,这个是浏览器提供的默认行为
  92. * 如果不希望发生这个行为,则可以通过return false来取消默认行为
  93. *
  94. * 但是这个方法对IE8不起作用
  95. */
  96. return false;
  97. };
  98. }
  99. </script>
  100. </head>
  101. <body>
  102. 我是一段文字
  103. <div id="box1"></div>
  104. <div id="box2"></div>
  105. <img src="img/zly.jpeg" id="img1" style="position: absolute;"/>
  106. </body>
  107. </html>