QSpopout.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * 弹出框类
  3. * @param title 标题
  4. * @constructor
  5. */
  6. function QSpopout(title) {
  7. this.title = title;
  8. this.init();
  9. }
  10. QSpopout.prototype = {
  11. /**
  12. * 初始化
  13. */
  14. init: function() {
  15. var popoutHtml = '<div id="popout" style="display: none;">';
  16. popoutHtml += '<div class="qs-mask"></div>';
  17. popoutHtml += '<div class="qs-popout">';
  18. if (this.title) {
  19. popoutHtml += '<div class="qs-popout-hd"><div class="qs-popout-title">' + this.title + '</div></div>';
  20. }
  21. popoutHtml += '<div class="qs-popout-bd"></div>';
  22. popoutHtml += '<div class="qs-popout-ft">';
  23. popoutHtml += '<a href="javascript:;" class="qs-popout-btn qs-popout-btn-default"></a>';
  24. popoutHtml += '<a href="javascript:;" class="qs-popout-btn qs-popout-btn-primary"></a>';
  25. popoutHtml += '</div>';
  26. popoutHtml += '</div>';
  27. popoutHtml += '</div>';
  28. $('body').append(popoutHtml);
  29. this.popout = $('#popout');
  30. // 设置默认按钮
  31. this.defaultBtn = this.getDefaultBtn();
  32. this.primaryBtn = this.getPrimaryBtn();
  33. this.setBtn(2, ['取消', '确定']);
  34. },
  35. /**
  36. * 显示
  37. */
  38. show: function() {
  39. this.popout.fadeIn(200);
  40. },
  41. /**
  42. * 设置内容
  43. * @param value 内容
  44. */
  45. setContent: function(value) {
  46. $('.qs-popout-bd').html(value);
  47. },
  48. /**
  49. * 隐藏
  50. */
  51. hide: function() {
  52. this.popout.fadeOut(200);
  53. this.popout.remove();
  54. },
  55. /**
  56. * 设置按钮
  57. * @param num 按钮数量
  58. * @param value 按钮内容
  59. */
  60. setBtn: function(num, value) {
  61. var that = this;
  62. if (num == 1) {
  63. this.defaultBtn.hide();
  64. value = value ? value : '确定';
  65. this.primaryBtn.text(value);
  66. } else {
  67. this.defaultBtn.text(value[0]);
  68. this.primaryBtn.text(value[1]);
  69. }
  70. // 关闭
  71. $('.qs-popout-btn').on('click', function() {
  72. that.hide();
  73. });
  74. },
  75. /**
  76. * 获取主操作按钮
  77. * @returns {*|jQuery|HTMLElement}
  78. */
  79. getPrimaryBtn: function() {
  80. return $('.qs-popout-btn-primary');
  81. },
  82. /**
  83. * 获取辅助操作按钮
  84. * @returns {*|jQuery|HTMLElement}
  85. */
  86. getDefaultBtn: function() {
  87. return $('.qs-popout-btn-default');
  88. }
  89. }