|
15 | 15 |
|
16 | 16 | var Modal = function (element, options) {
|
17 | 17 | this.options = options
|
| 18 | + this.$body = $(document.body) |
18 | 19 | this.$element = $(element)
|
19 | 20 | this.$backdrop =
|
20 | 21 | this.isShown = null
|
|
48 | 49 |
|
49 | 50 | this.isShown = true
|
50 | 51 |
|
| 52 | + this.$body.addClass('modal-open') |
| 53 | + |
| 54 | + this.setScrollbar() |
51 | 55 | this.escape()
|
52 | 56 |
|
53 | 57 | this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
|
|
56 | 60 | var transition = $.support.transition && that.$element.hasClass('fade')
|
57 | 61 |
|
58 | 62 | if (!that.$element.parent().length) {
|
59 |
| - that.$element.appendTo(document.body) // don't move modals dom position |
| 63 | + that.$element.appendTo(that.$body) // don't move modals dom position |
60 | 64 | }
|
61 | 65 |
|
62 | 66 | that.$element
|
|
96 | 100 |
|
97 | 101 | this.isShown = false
|
98 | 102 |
|
| 103 | + this.$body.removeClass('modal-open') |
| 104 | + |
| 105 | + this.resetScrollbar() |
99 | 106 | this.escape()
|
100 | 107 |
|
101 | 108 | $(document).off('focusin.bs.modal')
|
|
153 | 160 | var doAnimate = $.support.transition && animate
|
154 | 161 |
|
155 | 162 | this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
|
156 |
| - .appendTo(document.body) |
| 163 | + .appendTo(this.$body) |
157 | 164 |
|
158 | 165 | this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
|
159 | 166 | if (e.target !== e.currentTarget) return
|
|
188 | 195 | }
|
189 | 196 | }
|
190 | 197 |
|
| 198 | + Modal.prototype.setScrollbar = function () { |
| 199 | + if (document.body.clientHeight <= window.innerHeight) return |
| 200 | + var scrollbarWidth = this.measureScrollbar() |
| 201 | + var bodyPad = parseInt(this.$body.css('padding-right') || 0) |
| 202 | + if (scrollbarWidth) this.$body.css('padding-right', bodyPad + scrollbarWidth) |
| 203 | + } |
| 204 | + |
| 205 | + Modal.prototype.resetScrollbar = function () { |
| 206 | + this.$body.css('padding-right', '') |
| 207 | + } |
| 208 | + |
| 209 | + Modal.prototype.measureScrollbar = function () { // thx walsh |
| 210 | + var scrollDiv = document.createElement('div') |
| 211 | + scrollDiv.className = 'modal-scrollbar-measure' |
| 212 | + this.$body.append(scrollDiv) |
| 213 | + var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth |
| 214 | + this.$body[0].removeChild(scrollDiv) |
| 215 | + return scrollbarWidth |
| 216 | + } |
| 217 | + |
191 | 218 |
|
192 | 219 | // MODAL PLUGIN DEFINITION
|
193 | 220 | // =======================
|
|
236 | 263 | })
|
237 | 264 | })
|
238 | 265 |
|
239 |
| - $(document) |
240 |
| - .on('show.bs.modal', '.modal', function () { $(document.body).addClass('modal-open') }) |
241 |
| - .on('hidden.bs.modal', '.modal', function () { $(document.body).removeClass('modal-open') }) |
242 |
| - |
243 | 266 | }(jQuery);
|
0 commit comments