经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
js实现响应按钮点击弹出可拖拽的非模态对话框完整实例【测试可用】[原创]
来源:jb51  时间:2023/4/24 8:50:08  对本文有异议

1.css部分:

  1. .dialog {
  2. ? display: none;
  3. ? position: absolute;
  4. ? left: 50%;
  5. ? top: 50%;
  6. ? transform: translate(-50%, -50%);
  7. ? background-color: #fff;
  8. ? border-radius: 5px;
  9. ? box-shadow: 0 10px 20px rgba(0, 0, 0, .3);
  10. ? z-index: 9999;
  11. }
  12.  
  13. .dialog-header {
  14. ? background-color: #f6f7f8;
  15. ? padding: 10px;
  16. ? border-top-left-radius: 5px;
  17. ? border-top-right-radius: 5px;
  18. ? cursor: move;
  19. }
  20.  
  21. .dlgtitle {
  22. ? font-weight: bold;
  23. ? font-size: 16px;
  24. }
  25.  
  26. .close-btn {
  27. ? float: right;
  28. ? cursor: pointer;
  29. }
  30.  
  31. .dialog-content {
  32. ? padding: 20px;
  33. }

2.html部分:

  1. <button id="openBtn">打开对话框</button>
  2.  
  3. <div id="dialog" class="dialog">
  4. ? <div class="dialog-header">
  5. ? ? <span class="dlgtitle">对话框标题</span>
  6. ? ? <span class="close-btn">&times;</span>
  7. ? </div>
  8. ? <div class="dialog-content">
  9. ? ? <p>这里是对话框内容</p>
  10. ? </div>
  11. </div>

3.JavaScript部分:

  1. var dialog = document.getElementById('dialog');
  2. var openBtn = document.getElementById('openBtn');
  3. var closeBtn = document.querySelector('.close-btn');
  4. var isDragging = false;
  5. var mouseX, mouseY, dialogLeft, dialogTop;
  6.  
  7. // 鼠标按下时记录鼠标位置以及对话框位置
  8. dialogHeaderMouseDown = function(e) {
  9. ? isDragging = true;
  10. ? mouseX = e.clientX;
  11. ? mouseY = e.clientY;
  12. ? dialogLeft = dialog.offsetLeft;
  13. ? dialogTop = dialog.offsetTop;
  14. }
  15.  
  16. // 鼠标移动时移动对话框
  17. // document.onmousemove = function(e) {
  18. dialogHeaderMouseMove = function(e) {
  19. ? if (isDragging) {
  20. ? ? var moveX = e.clientX - mouseX;
  21. ? ? var moveY = e.clientY - mouseY;
  22. ? ? dialog.style.left = dialogLeft + moveX + 'px';
  23. ? ? dialog.style.top = dialogTop + moveY + 'px';
  24. ? }
  25. }
  26.  
  27. // 鼠标松开时停止拖动
  28. // document.onmouseup = function() {
  29. dialogHeaderMouseup = function() {
  30. ? isDragging = false;
  31. }
  32.  
  33. // 点击打开按钮显示对话框
  34. openBtn.onclick = function() {
  35. ? dialog.style.display = 'block';
  36. }
  37.  
  38. // 点击关闭按钮或对话框外部关闭对话框
  39. closeBtn.onclick = function() {
  40. ? dialog.style.display = 'none';
  41. }
  42.  
  43. dialog.onclick = function(e) {
  44. ? if (e.target == dialog) {
  45. ? ? dialog.style.display = 'none';
  46. ? }
  47. }
  48.  
  49. // 鼠标按下对话框头部,开始拖动对话框
  50. var header = document.querySelector('.dialog-header');
  51. header.addEventListener('mousedown', dialogHeaderMouseDown);
  52. header.addEventListener('mousemove', dialogHeaderMouseMove);
  53. header.addEventListener('mouseup', dialogHeaderMouseup);

可以使用jb51在线工具:http://tools.jb51.net/code/WebCodeRun 测试上述代码运行效果。

附:完整示例代码:

  1. <!DOCTYPE html>
  2. <html lang="zh_CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>可拖拽非模态对话框</title>
  7. <style>
  8. .dialog {
  9. display: none;
  10. position: absolute;
  11. left: 50%;
  12. top: 50%;
  13. transform: translate(-50%, -50%);
  14. background-color: #fff;
  15. border-radius: 5px;
  16. box-shadow: 0 10px 20px rgba(0, 0, 0, .3);
  17. z-index: 9999;
  18. }
  19.  
  20. .dialog-header {
  21. background-color: #f6f7f8;
  22. padding: 10px;
  23. border-top-left-radius: 5px;
  24. border-top-right-radius: 5px;
  25. cursor: move;
  26. }
  27.  
  28. .dlgtitle {
  29. font-weight: bold;
  30. font-size: 16px;
  31. }
  32.  
  33. .close-btn {
  34. float: right;
  35. cursor: pointer;
  36. }
  37.  
  38. .dialog-content {
  39. padding: 20px;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <button id="openBtn">打开对话框</button>
  45. <div id="dialog" class="dialog">
  46. <div class="dialog-header">
  47. <span class="dlgtitle">对话框标题</span>
  48. <span class="close-btn">&times;</span>
  49. </div>
  50. <div class="dialog-content">
  51. <p>这里是对话框内容</p>
  52. </div>
  53. </div>
  54. <script>
  55. var dialog = document.getElementById('dialog');
  56. var openBtn = document.getElementById('openBtn');
  57. var closeBtn = document.querySelector('.close-btn');
  58. var isDragging = false;
  59. var mouseX, mouseY, dialogLeft, dialogTop;
  60.  
  61. // 鼠标按下时记录鼠标位置以及对话框位置
  62. dialogHeaderMouseDown = function(e) {
  63. isDragging = true;
  64. mouseX = e.clientX;
  65. mouseY = e.clientY;
  66. dialogLeft = dialog.offsetLeft;
  67. dialogTop = dialog.offsetTop;
  68. }
  69.  
  70. // 鼠标移动时移动对话框
  71. // document.onmousemove = function(e) {
  72. dialogHeaderMouseMove = function(e) {
  73. if (isDragging) {
  74. var moveX = e.clientX - mouseX;
  75. var moveY = e.clientY - mouseY;
  76. dialog.style.left = dialogLeft + moveX + 'px';
  77. dialog.style.top = dialogTop + moveY + 'px';
  78. }
  79. }
  80.  
  81. // 鼠标松开时停止拖动
  82. // document.onmouseup = function() {
  83. dialogHeaderMouseup = function() {
  84. isDragging = false;
  85. }
  86.  
  87. // 点击打开按钮显示对话框
  88. openBtn.onclick = function() {
  89. dialog.style.display = 'block';
  90. }
  91.  
  92. // 点击关闭按钮或对话框外部关闭对话框
  93. closeBtn.onclick = function() {
  94. dialog.style.display = 'none';
  95. }
  96.  
  97. dialog.onclick = function(e) {
  98. if (e.target == dialog) {
  99. dialog.style.display = 'none';
  100. }
  101. }
  102.  
  103. // 鼠标按下对话框头部,开始拖动对话框
  104. var header = document.querySelector('.dialog-header');
  105. header.addEventListener('mousedown', dialogHeaderMouseDown);
  106. header.addEventListener('mousemove', dialogHeaderMouseMove);
  107. header.addEventListener('mouseup', dialogHeaderMouseup);
  108. </script>
  109. </body>
  110. </html>

还可以使用jb51在线工具:http://tools.jb51.net/code/HtmlJsRun 测试上述代码运行效果!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号