一、原生 js 实现

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <link rel="stylesheet" href="css/reset.min.css">
  7. <style>
  8. html,
  9. body {
  10. height: 100%;
  11. overflow: hidden;
  12. }
  13. #box {
  14. position: absolute;
  15. top: 0;
  16. left: 0;
  17. width: 100px;
  18. height: 100px;
  19. background: red;
  20. }
  21. #box h2 {
  22. height: 30px;
  23. background: green;
  24. cursor: move;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="box">
  30. <h2></h2>
  31. </div>
  32. <script>
  33. let css = function (curEle, attr) {
  34. return parseFloat(window.getComputedStyle(curEle, null)[attr]);
  35. };
  36. let down = function (ev) {
  37. this.startX = ev.pageX;
  38. this.startY = ev.pageY;
  39. this.startL = css(this, 'left');
  40. this.startT = css(this, 'top');
  41. this._MOVE = move.bind(this);
  42. this._UP = up.bind(this);
  43. document.addEventListener('mousemove', this._MOVE);
  44. document.addEventListener('mouseup', this._UP);
  45. };
  46. let move = function (ev) {
  47. let curL = ev.pageX - this.startX + this.startL,
  48. curT = ev.pageY - this.startY + this.startT;
  49. let minL = 0,
  50. minT = 0,
  51. maxL = document.documentElement.clientWidth - this.offsetWidth,
  52. maxT = document.documentElement.clientHeight - this.offsetHeight;
  53. curL = curL < minL ? minL : (curL > maxL ? maxL : curL);
  54. curT = curT < minT ? minT : (curT > maxT ? maxT : curT);
  55. this.style.left = curL + 'px';
  56. this.style.top = curT + 'px';
  57. };
  58. let up = function (ev) {
  59. document.removeEventListener('mousemove', this._MOVE);
  60. document.removeEventListener('mouseup', this._UP);
  61. };
  62. box.addEventListener('mousedown', down);
  63. </script>
  64. </body>
  65. </html>

二、HTML5 中 DRAG 事件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <link rel="stylesheet" href="css/reset.min.css">
  7. <style>
  8. html,
  9. body {
  10. height: 100%;
  11. overflow: hidden;
  12. }
  13. #box {
  14. position: absolute;
  15. top: 0;
  16. left: 0;
  17. z-index: 10;
  18. width: 100px;
  19. height: 100px;
  20. background: red;
  21. cursor: move;
  22. }
  23. #container {
  24. position: relative;
  25. box-sizing: border-box;
  26. margin: 30px auto;
  27. width: 300px;
  28. height: 200px;
  29. border: 2px solid lightseagreen;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="box" draggable="true"></div>
  35. <div id="container"></div>
  36. <!--
  37. HTML5中DRAG事件:可以把一个元素从当前位置拖拽到指定的容器中
  38. dragstart
  39. drag
  40. dragend
  41. dragover 拖动元素到指定的目标区域上
  42. drop 可以把拖动元素放到目标区域中了
  43. 1. 给要拖拽的元素设置可被拖拽属性
  44. draggable = "true"
  45. 2. 在拖拽开始的时候,记录一些信息
  46. -->
  47. <script>
  48. box.ondragstart = function (ev) {
  49. // DragEvent:拖拽事件对象
  50. // dataTransfer(DataTransfer):setData / getData / clearData 设置的内容最后都会变为字符串 setData(类型标识,对应的值)
  51. ev.dataTransfer.setData('@A', ev.target.id);
  52. }
  53. container.ondragover = function (ev) {
  54. ev.preventDefault();
  55. }
  56. container.ondrop = function (ev) {
  57. ev.preventDefault();
  58. let _ID = ev.dataTransfer.getData('@A'),
  59. _ELE = document.getElementById(_ID);
  60. container.appendChild(_ELE);
  61. }
  62. </script>
  63. </body>
  64. </html>