拖动div元素

蔚落 2023-06-01 12:38 103阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>CodePen - draggable div#4</title>
  6. <style>
  7. *{
  8. margin:0;padding:0;border:none}
  9. body,html{
  10. height:100%;width:100%}
  11. .drag-box{
  12. user-select:none;background:#f0f0f0;z-index:2147483647;position:fixed;left:0;top:0;width:200px}
  13. #dragBoxBar{
  14. align-items:center;display:flex;justify-content:space-between;background:#ccc;width:100%;height:40px;cursor:move;user-select:none}
  15. .no-select{
  16. user-select:none}
  17. .pointer-events{
  18. pointer-events:none}
  19. .no-border{
  20. border:none}
  21. #injectedBox{
  22. height:160px;display:flex;align-items:center;justify-content:center;font-size:2rem;background:#eee}
  23. #dragdiv{
  24. z-index:2147483647;position:fixed;left:0;top:0;width:200px}
  25. #draghead {
  26. background:#ccc;width:100%;height:40px;cursor:move}
  27. #dragbody{
  28. height:160px;width:100%;background:#eee}
  29. </style>
  30. <script src="jquery-1.8.2.min.js"></script>
  31. </head>
  32. <body>
  33. <div id="dragdiv">
  34. <div id="draghead"></div>
  35. <div id="dragbody"></div>
  36. </div>
  37. <script>
  38. var isMouseDown,
  39. initX,
  40. initY,
  41. height = $('#dragbody').offsetHeight,
  42. width = $('#dragbody').offsetWidth,
  43. dragBoxBar = document.getElementById('draghead'),
  44. injectedBox = document.getElementById('dragbody'),
  45. dragBox = document.getElementById('dragdiv');
  46. dragBoxBar.addEventListener('mousedown', function (e) {
  47. isMouseDown = true;
  48. document.body.classList.add('no-select');
  49. injectedBox.classList.add('pointer-events');
  50. initX = e.offsetX;
  51. initY = e.offsetY;
  52. //dragBox.style.opacity = 0.5;
  53. });
  54. dragBoxBar.addEventListener('mouseup', function (e) {
  55. mouseupHandler();
  56. });
  57. document.addEventListener('mousemove', function (e) {
  58. if (isMouseDown) {
  59. var cx = e.clientX - initX,
  60. cy = e.clientY - initY;
  61. if (cx < 0) cx = 0;
  62. if (cy < 0) cy = 0;
  63. if (window.innerWidth - e.clientX + initX < width + 16) cx = window.innerWidth - width;
  64. if (e.clientY > window.innerHeight - height - dragBoxBar.offsetHeight + initY) cy = window.innerHeight - dragBoxBar.offsetHeight - height;
  65. dragBox.style.left = cx + 'px';
  66. dragBox.style.top = cy + 'px';
  67. }
  68. });
  69. document.addEventListener('mouseup', function (e) {
  70. if (e.clientY > window.innerWidth || e.clientY < 0 || e.clientX < 0 || e.clientX > window.innerHeight) {
  71. mouseupHandler();
  72. }
  73. });
  74. function mouseupHandler() {
  75. isMouseDown = false;
  76. document.body.classList.remove('no-select');
  77. injectedBox.classList.remove('pointer-events');
  78. //dragBox.style.opacity = 1;
  79. }
  80. </script>
  81. </body>
  82. </html>

转载于:https://www.cnblogs.com/wangyongx/p/11526783.html

发表评论

表情:
评论列表 (有 0 条评论,103人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js 鼠标拖动div

    div中心跟踪鼠标动而动 得到鼠标坐标,移动时div的左上距离为鼠标两坐标减去div左上顶点到中心的距离 div任意位置随鼠标动而动 得到鼠标第一次坐标,移动时div