본문 바로가기

BootStrap/Bootstrap Sample File

HOW TO MAKE MODAL (POPUP/DIALOG) USING JQUERY

How to Make Modal (Popup/Dialog) Using jQuery-In this article I will discuss how to make modals popup using jquery, jquery to get the plugin, you can download on their official website.




Create a new folder with a name up to you, but it is recommended to use the name of "modal". then copy the jquery plugins into the folder "modals". Next make a new file files with names such as index.html, style.css and script.js

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html>
<head>
  <meta <span id="IL_AD6" class="IL_AD">charset</span>="utf-8"/>
  <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
  <title>jQuery Modal</title>
</head>
<body>
<p>
</p>
<a href="#ex1" rel="modal:open"><<span id="IL_AD9" class="IL_AD">button</span>>Open & Close with links</button></a>
<a href="#ex2" rel="modal:open"><button>Styled login form & events</button></a>
<a href="#ex3" rel="modal:open"><button>Resizing</button></a>
<a href="#ex7" rel="modal:open"><button>Fade Transitions</button></a>
<a href="#ex8" rel="modal:open"><button>Fade In</button></a>
<a href="#ex9" rel="modal:open"><button>Finished Transitioning</button></a>
<p>
</p>
<!-- Modal HTML embedded directly into <span id="IL_AD8" class="IL_AD">document</span> -->
<div class="modal" id="ex1" style="display:none;">
  <p>Thanks for clicking.  That felt good. 
Click <a href="#" rel="modal:close">close</a>, click the overlay, or press ESC</p>
</div>
 
<!-- Modal HTML embedded directly into document -->
<form action="" class="login_form modal" id="ex2" style="display:none;">
  <h3>Please login to continue</h3>
  <p><label>Username:</label><input type="text" /></p>
  <p><label>Password:</label><input type="password" /></p>
  <p><input type="<span id="IL_AD3" class="IL_AD">submit</span>" value="Login" /></p>
</form>
 
<div id="ex3" class="modal">
  <p><a id="more" href="#more">More!</a></p>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
 
<div id="ex7" class="modal">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
 
<div id="ex8" class="modal">
  <p>This modal starts fading in once the overlay is 50% faded in.</p>
</div>
 
<div id="ex9" class="modal">
  <p>This modal starts fading in well after the overlay has finished transitioning.</p>
</div>
 
<style type="text/css">
  .modal a.close-modal[class*="icon-"] {
    top: -10px;
    right: -10px;
    width: 20px;
    height: 20px;
    color: #fff;
    line-height: 1.25;
    text-align: center;
    text-decoration: none;
    text-indent: 0;
    background: #900;
    border: 2px solid #fff;
    -webkit-border-radius: 26px;
    -moz-border-radius: 26px;
    -o-border-radius: 26px;
    -ms-border-radius: 26px;
    -moz-box-shadow:    1px 1px 5px rgba(0,0,0,0.5);
    -webkit-box-shadow: 1px 1px 5px rgba(0,0,0,0.5);
    box-shadow:         1px 1px 5px rgba(0,0,0,0.5);
  }
  button{
    background: #393939;
    color:#fff;
    border:1px solid #6e6e6e;
    border-radius:3px;
    padding:5px 10px;
    cursor:pointer;
  }
  button:hover{
    background:#fff;
    color:#393939;
  }
</style>
 
<script src="jquery.min.js" type="text/javascript"></script>
<script src="script.js" type="text/javascript"></script>
 
<script type="text/javascript" charset="utf-8">
  $(function() {
 
    function log_modal_event(event, modal) {
      if(typeof console != 'undefined' && console.log) console.log("[event] " + event.type);
    };
 
    $(document).on($.modal.BEFORE_BLOCK, log_modal_event);
    $(document).on($.modal.BLOCK, log_modal_event);
    $(document).on($.modal.BEFORE_OPEN, log_modal_event);
    $(document).on($.modal.OPEN, log_modal_event);
    $(document).on($.modal.BEFORE_CLOSE, log_modal_event);
    $(document).on($.modal.CLOSE, log_modal_event);
    $(document).on($.modal.AJAX_SEND, log_modal_event);
    $(document).on($.modal.AJAX_SUCCESS, log_modal_event);
    $(document).on($.modal.AJAX_COMPLETE, log_modal_event);
 
    $('#more').click(function() {
      $(this).parent().after($(this).parent().next().clone());
      $.modal.resize();
      return false;
    });
 
    $('#manual-ajax').click(function(event) {
      event.preventDefault();
      $.get(this.href, function(html) {
        $(html).appendTo('body').modal();
      });
    });
 
    $('a[href="#ex5"]').click(function(event) {
      event.preventDefault();
      $(this).modal({
        escapeClose: false,
        clickClose: false,
        showClose: false
      });
    });
 
    $('a[href="#ex7"]').click(function(event) {
      event.preventDefault();
      $(this).modal({
        fadeDuration: 250
      });
    });
 
    $('a[href="#ex8"]').click(function(event) {
      event.preventDefault();
      $(this).modal({
        fadeDuration: 1000,
        fadeDelay: 0.50
      });
    });
 
    $('a[href="#ex9"]').click(function(event) {
      event.preventDefault();
      $(this).modal({
        fadeDuration: 1000,
        fadeDelay: 1.75
      });
    });
 
    $('a[href="#ex10"]').click(function(event){
      event.preventDefault();
      $(this).modal({
        closeClass: 'icon-remove',
        closeText: '!'
      });
    });
 
  });
</script>
</body>
</html>


style.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
.modal {
  display: none;
  width: 400px;
  background: #fff;
  padding: 15px 30px;
  -webkit-border-radius: 8px;
  -moz-border-radius: 8px;
  -o-border-radius: 8px;
  -ms-border-radius: 8px;
  border-radius: 8px;
  -webkit-box-shadow: 0 0 10px #000;
  -moz-box-shadow: 0 0 10px #000;
  -o-box-shadow: 0 0 10px #000;
  -ms-box-shadow: 0 0 10px #000;
  box-shadow: 0 0 10px #000;
}
.modal a.close-modal {
  position: absolute;
  top: -12.5px;
  right: -12.5px;
  display: block;
  width: 30px;
  height: 30px;
  text-indent: -9999px;
  background: url(http://kylefox.ca/jquery-modal/close.png) no-repeat 0 0;
}
.modal-spinner {
  display: none;
  width: 64px;
  height: 64px;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-right: -32px;
  margin-top: -32px;
  background: url(http://kylefox.ca/jquery-modal/spinner.gif) #111 no-repeat center center;
  -webkit-border-radius: 8px;
  -moz-border-radius: 8px;
  -o-border-radius: 8px;
  -ms-border-radius: 8px;
  border-radius: 8px;
}


script.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Version 0.5.5
*/
(function($) {
  var current = null;
  $.modal = function(el, options) {
    $.modal.close(); // Close any open modals.
    var remove, target;
    this.$body = $('body');
    this.options = $.extend({}, $.modal.defaults, options);
    this.options.doFade = !isNaN(parseInt(this.options.fadeDuration, 10));
    if (el.is('a')) {
      target = el.attr('href');
      //<span id="IL_AD10" class="IL_AD">Select</span> element by id from href
      if (/^#/.test(target)) {
 this.$elm = $(target);
 if (this.$elm.length !== 1) return null;
   this.open();
   //AJAX
 } else {
   this.$elm = $('<div>');
   this.$body.append(this.$elm);
   remove = function(event, modal) { modal.elm.remove(); };
   this.showSpinner();
   el.trigger($.modal.AJAX_SEND);
   $.get(target).done(function(html) {
     if (!current) return;
     el.trigger($.modal.AJAX_SUCCESS);
     current.$elm.empty().append(html).on($.modal.CLOSE, remove);
     current.hideSpinner();
     current.open();
     el.trigger($.modal.AJAX_COMPLETE);
   }).fail(function() {
     el.trigger($.modal.AJAX_FAIL);
     current.hideSpinner();
     el.trigger($.modal.AJAX_COMPLETE);
   });
 }
      } else {
 this.$elm = el;
 this.$body.append(this.$elm);
 this.open();
      }
    };
    $.modal.prototype = {
      constructor: $.modal,
      open: function() {
 var m = this;
 if(this.options.doFade) {
   this.block();
   setTimeout(function() {
     m.show();
   }, this.options.fadeDuration * this.options.fadeDelay);
 } else {
 this.block();
 this.show();
      }
      if (this.options.escapeClose) {
 $(document).on('keydown.modal', function(event) {
   if (event.which == 27) $.modal.close();
 });
      }
      if (this.options.clickClose) this.blocker.click($.modal.close);
    },
    close: function() {
      this.<span id="IL_AD11" class="IL_AD">unblock</span>();
      this.hide();
      $(document).off('keydown.modal');
    },
    block: function() {
      var initialOpacity = this.options.doFade ? 0 : this.options.opacity;
      this.$elm.trigger($.modal.BEFORE_BLOCK, [this._ctx()]);
      this.blocker = $('<div class="jquery-modal blocker"></div>').css({
 top: 0, right: 0, bottom: 0, left: 0,
 width: "100%", height: "100%",
 position: "fixed",
 zIndex: this.options.zIndex,
 background: this.options.overlay,
 opacity: initialOpacity
      });
      this.$body.append(this.blocker);
      if(this.options.doFade) {
 this.blocker.animate({opacity: this.options.opacity}, this.options.fadeDuration);
      }
      this.$elm.trigger($.modal.BLOCK, [this._ctx()]);
    },
    unblock: function() {
      if(this.options.doFade) {
 this.blocker.fadeOut(this.options.fadeDuration, function() {
   $(this).remove();
 });
      } else {
 this.blocker.remove();
      }
    },
    show: function() {
      this.$elm.trigger($.modal.BEFORE_OPEN, [this._ctx()]);
      if (this.options.showClose) {
 this.closeButton = $('<a href="#close-modal" rel="modal:close" class="close-modal ' + this.options.closeClass + '">' + this.options.closeText + '</a>');
 this.$elm.append(this.closeButton);
      }
      this.$elm.addClass(this.options.modalClass + ' current');
      this.center();
      if(this.options.doFade) {
 this.$elm.fadeIn(this.options.fadeDuration);
      } else {
 this.$elm.show();
      }
      this.$elm.trigger($.modal.OPEN, [this._ctx()]);
    },
    hide: function() {
      this.$elm.trigger($.modal.BEFORE_CLOSE, [this._ctx()]);
      if (this.closeButton) this.closeButton.remove();
      this.$elm.removeClass('current');
      if(this.options.doFade) {
      this.$elm.fadeOut(this.options.fadeDuration);
    } else {
      this.$elm.hide();
    }
    this.$elm.trigger($.modal.CLOSE, [this._ctx()]);
  },
  showSpinner: function() {
    if (!this.options.showSpinner) return;
    this.spinner = this.spinner || $('<div class="' + this.options.modalClass + '-spinner"></div>').append(this.options.spinnerHtml);
    this.$body.append(this.spinner);
    this.spinner.show();
  },
  hideSpinner: function() {
    if (this.spinner) this.spinner.remove();
  },
  center: function() {
    this.$elm.css({
      position: 'fixed',
      top: "50%",
      left: "50%",
      marginTop: - (this.$elm.outerHeight() / 2),
      marginLeft: - (this.$elm.outerWidth() / 2),
      zIndex: this.options.zIndex + 1
    });
  },
  //Return context for custom events
  _ctx: function() {
    return { elm: this.$elm, blocker: this.blocker, options: this.options };
  }
};
//resize is alias for center for now
$.modal.prototype.resize = $.modal.prototype.center;
$.modal.close = function(event) {
  if (!current) return;
  if (event) event.preventDefault();
  current.close();
  var that = current.$elm;
  current = null;
  return that;
};
$.modal.resize = function() {
  if (!current) return;
  current.resize();
};
// Returns if there currently is an active modal
$.modal.isActive = function () {
  return current ? true : false;
}
$.modal.defaults = {
  overlay: "#000",
  opacity: 0.75,
  zIndex: 1,
  escapeClose: true,
  clickClose: true,
  closeText: 'Close',
  closeClass: '',
  modalClass: "modal",
  spinnerHtml: null,
  showSpinner: true,
  showClose: true,
  fadeDuration: null, // Number of milliseconds the fade animation takes.
  fadeDelay: 1.0 // Point during the overlay's fade-in that the modal begins to fade in (.5 = 50%, 1.5 = 150%, etc.)
};
// Event constants
$.modal.BEFORE_BLOCK = 'modal:before-block';
$.modal.BLOCK = 'modal:block';
$.modal.BEFORE_OPEN = 'modal:before-open';
$.modal.OPEN = 'modal:open';
$.modal.BEFORE_CLOSE = 'modal:before-close';
$.modal.CLOSE = 'modal:close';
$.modal.AJAX_SEND = 'modal:ajax:send';
$.modal.AJAX_SUCCESS = 'modal:ajax:success';
$.modal.AJAX_FAIL = 'modal:ajax:fail';
$.modal.AJAX_COMPLETE = 'modal:ajax:complete';
$.fn.modal = function(options){
  if (this.length === 1) {
    current = new $.modal(this, options);
  }
  return this;
};
// Automatically bind links with rel="modal:close" to, well, close the modal.
$(document).on('click.modal', 'a[rel="modal:close"]', $.modal.close);
$(document).on('click.modal', 'a[rel="modal:open"]', function(event) {
  event.preventDefault();
  $(this).modal();
});
})(jQuery);