【jQuery】jQuery弹出框可拖拽移动

忘是亡心i 2022-10-07 06:48 316阅读 0赞

1.说明

此demo需要用到jQuery和bootstrap,使用前需要先引入相关文件
因为内容比较简单,所以就不多说明了

2.相关代码

位置排版使用了绝对定位,部分是写死的,实际情况应该修改为动态自适应

  1. <div id="windowWithdraw">
  2. <div id="windowTitle">
  3. <span id="windowClose">关闭</span>
  4. <div>提现状态重置</div>
  5. </div>
  6. <windowFrom>
  7. 这是一个弹出框
  8. </windowFrom>
  9. </div>
  10. #windowWithdraw {
  11. border: 1px solid #2e2e2e;
  12. /* width: 720px; */
  13. height: 700px;
  14. background-color: white;
  15. border-radius: 5px;
  16. margin-left: 30px;
  17. margin-top: 20px;
  18. position: absolute;
  19. left: 50%;
  20. top: 50%;
  21. margin-left: -360px;
  22. margin-top: -350px;
  23. display: none;
  24. z-index: 1500;
  25. padding-right: 0 !important; /* 此处是因为bootstrap会带来padding-rught:17px */
  26. }
  27. #windowFrom {
  28. padding: 50px;
  29. }
  30. #windowClose {
  31. float: right;
  32. }
  33. #windowClose:hover {
  34. color: red;
  35. cursor: pointer;
  36. }
  37. #windowTitle {
  38. background-color: #343a40;
  39. font-size: 14px;
  40. padding: 4px;
  41. cursor: move;
  42. color: white;
  43. }
  44. // 该方法弹窗并进入模态,在需要的地方调用此方法
  45. function showWindowWithdraw(data) {
  46. $('#windowWithdraw').css({
  47. left: "50%",
  48. top: "50%",
  49. "margin-left": "-360px",
  50. "margin-top": "-350px"
  51. })
  52. $('#windowWithdraw').modal('show')
  53. $('#windowWithdraw').show();
  54. }
  55. // 点击关闭按钮
  56. $('#windowClose').click(function() {
  57. hideWindowWithdraw()
  58. });
  59. // 按下鼠标移动弹窗位置
  60. $('#windowTitle').mousedown(function(event) {
  61. var isMove = true;
  62. $(document).mousemove(function(event) {
  63. if (isMove) {
  64. var obj = $('#windowWithdraw');
  65. obj.css({
  66. 'left': event.pageX,
  67. 'top': event.pageY + 330
  68. });
  69. };
  70. }).mouseup(function(event) {
  71. isMove = false;
  72. });;
  73. });
  74. //点击其他区域隐藏窗口
  75. $(document).mouseup(e => {
  76. let window = $('#windowWithdraw');
  77. $(document).click(function(e) {
  78. var drag = $("#windowWithdraw"),
  79. dragel = $("#windowWithdraw")[0],
  80. target = e.target;
  81. if (dragel !== target && !$.contains(dragel, target)) {
  82. hideWindowWithdraw()
  83. }
  84. });
  85. });
  86. // 隐藏模态框
  87. function hideWindowWithdraw() {
  88. $('#windowWithdraw').modal('hide')
  89. $('.modal-backdrop').remove(); //去掉遮罩层
  90. $('#windowWithdraw').hide();
  91. }

提示:在弹窗内有点击按钮,可能会存在冒泡影响,可以适当阻止冒泡事件

发表评论

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

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

相关阅读