js制作弹窗

不念不忘少年蓝@ 2024-04-18 18:40 138阅读 0赞

个人见解,欢迎大家一起交流学习n(*≧▽≦*)n

效果图:

2019091016350945.gif

html页面

  1. <body>
  2. <button class="clickButton confirm">点击</button>
  3. <div style="display: none" name="addImageInfo-con">
  4. 这是内容
  5. </div>
  6. <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
  7. <script src="./js/iconfont.js"></script>
  8. <script src="./js/popup.js"></script>
  9. <script>
  10. $("button").click(function () {
  11. let diaLog = $.Dialog({
  12. title: "test",
  13. "content" : $("div[name=addImageInfo-con]").html(),
  14. button: [
  15. {
  16. txt: "确认",
  17. callback: function () {
  18. // diaLog.close();
  19. }
  20. }, {
  21. txt: "取消",
  22. className: "cancel"
  23. }
  24. ]
  25. })
  26. })
  27. </script>
  28. </body>

popup.js

  1. (function ($) {
  2. $.extend({
  3. Dialog: function (options) {
  4. let config = $.extend({
  5. title: "",
  6. button: [],
  7. content:""
  8. }, options);
  9. let $container = $('<div class="dialog" ></div>');
  10. let dialog = new Dialog($container, config);
  11. return dialog;
  12. }
  13. })
  14. function Dialog($container, config) {
  15. this.init($container, config)
  16. }
  17. Dialog.prototype = {
  18. init: function ($container, config) {
  19. this.$container = $container;
  20. this.config = config;
  21. let that = this;
  22. $container.appendTo($('body'));
  23. let $div = $('<div class="popup">\n' +
  24. ' <div class="popupTop">\n' +
  25. ' <div class="popTitle">弹窗</div>\n' +
  26. ' <div class="maximize">\n' +
  27. ' <svg class="icon" aria-hidden="true">\n' +
  28. ' <use xlink:href="#icon-kelong"></use>\n' +
  29. ' </svg>\n' +
  30. ' </div>\n' +
  31. ' <div class="popClose">\n' +
  32. ' <svg class="icon" aria-hidden="true">\n' +
  33. ' <use xlink:href="#icon-guanbi1"></use>\n' +
  34. ' </svg>\n' +
  35. ' </div>\n' +
  36. ' </div>\n' +
  37. ' <div class="content"></div>\n' +
  38. ' <div class="divButton">\n' +
  39. ' <div class="button">\n' +
  40. ' </div>\n' +
  41. ' </div>\n' +
  42. ' </div>\n' +
  43. ' <div class="containBack"></div>');
  44. this.$container.append($div);
  45. $(".popTitle").html(this.config.title);
  46. $(".content").html(this.config.content);
  47. let button = document.getElementsByClassName("button")[0];
  48. for (let i = 0; i < this.config.button.length; i++) {
  49. if (typeof (this.config.button[i].className) != "undefined") {
  50. button.innerHTML = button.innerHTML + '<button id="buttonId' + i + '" class="defaultButton ' + this.config.button[i].className + '">' + this.config.button[i].txt + '</button>';
  51. } else {
  52. button.innerHTML = button.innerHTML + '<button id="buttonId' + i + '" class="defaultButton confirm">' + this.config.button[i].txt + '</button>';
  53. }
  54. }
  55. $(".defaultButton").click(function () {
  56. let id = $(this).attr('id');
  57. let callback = that.config.button[id.substring(8)].callback;
  58. if (typeof (callback) === 'function') {
  59. callback();
  60. } else {
  61. that.close();
  62. }
  63. })
  64. $(".popClose").click(function () {
  65. that.close();
  66. })
  67. $(".maximize").click(function () {
  68. $(".popup").toggleClass("popupMax");
  69. })
  70. $(".popupTop").mousedown(function (event) {
  71. let left = $('.popup').position().left;
  72. let top = $('.popup').position().top;
  73. let clientX = event.clientX;
  74. let clientY = event.clientY;
  75. $(".popupTop").mousemove(function (even) {
  76. $(".popup").css("left", (even.clientX - clientX + left) + "px");
  77. $(".popup").css("top", (even.clientY - clientY + top) + "px");
  78. })
  79. })
  80. $(".popupTop").mouseup(function () {
  81. $(".popupTop").unbind("mousemove")
  82. })
  83. },
  84. close: function () {
  85. $(".dialog").remove();
  86. }
  87. }
  88. })(jQuery);

popup.css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .icon {
  6. width: 1em; height: 1em;
  7. vertical-align: -0.15em;
  8. fill: currentColor;
  9. overflow: hidden;
  10. }
  11. html, body {
  12. width: 100%;
  13. height: 100%;
  14. overflow: hidden;
  15. }
  16. .dialog {
  17. width: 100%;
  18. height: 100%;
  19. }
  20. .containBack {
  21. position: fixed;
  22. top: 0;
  23. left: 0;
  24. width: 100%;
  25. height: 100%;
  26. z-index: 9999;
  27. background: rgba(3, 20, 32, 1) none repeat scroll 0 0;
  28. opacity: 0.3;
  29. }
  30. .popup {
  31. position: absolute;
  32. background-color: white;
  33. z-index: 10000;
  34. border-radius: 10px;
  35. width: 580px;
  36. height: 380px;
  37. left: 50%;
  38. top: 50%;
  39. margin-left: -290px;
  40. margin-top: -190px;
  41. }
  42. .popupMax {
  43. width: 860px;
  44. height: 540px;
  45. left: 50%;
  46. top: 50%;
  47. margin-left: -430px;
  48. margin-top: -270px;
  49. }
  50. .popupTop {
  51. width: 100%;
  52. height: 50px;
  53. background-color: #38455A;
  54. border-top-right-radius: 5px;
  55. border-top-left-radius: 5px;
  56. cursor: move;
  57. }
  58. .popTitle {
  59. position: absolute;
  60. color: #ffffff;
  61. line-height: 50px;
  62. left: 10px;
  63. }
  64. .popClose {
  65. position: absolute;
  66. color: #ffffff;
  67. top: 18px;
  68. right: 18px;
  69. font-size: 14px;
  70. }
  71. .popClose,.maximize:hover {
  72. cursor: pointer;
  73. }
  74. .maximize {
  75. position: absolute;
  76. color: #ffffff;
  77. top: 18px;
  78. right: 50px;
  79. font-size: 16px;
  80. }
  81. .content {
  82. width: 100%;
  83. height: calc(100% - 112px);
  84. border-bottom-right-radius: 5px;
  85. border-bottom-left-radius: 5px;
  86. }
  87. .defaultButton {
  88. border: none;
  89. background-color: transparent;
  90. outline: none;
  91. }
  92. .divButton {
  93. position: relative;
  94. height: 62px;
  95. border-top: 1px solid #E9ECEF;
  96. }
  97. .button {
  98. position: absolute;
  99. right: 15px;
  100. }
  101. .button button{
  102. margin-right: 15px;
  103. height: 32px;
  104. line-height: 32px;
  105. font-size: 14px;
  106. border-radius: 2px;
  107. padding: 0 20px;
  108. margin-top: 15px;
  109. cursor: pointer;
  110. }
  111. .confirm {
  112. background-color: #1188DD;
  113. color: #ffffff;
  114. margin-right: 20px !important;
  115. }
  116. .confirm:hover {
  117. background-color: #004499;
  118. }
  119. .cancel {
  120. background: #EEF7FF;
  121. color: #1188DD;
  122. }
  123. .cancel:hover {
  124. background: #DDEEFF;
  125. }

发表评论

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

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

相关阅读