Skip to content

Commit 615dcb5

Browse files
committed
fixes twbs#9855
1 parent bdb70fa commit 615dcb5

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

js/modal.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
var Modal = function (element, options) {
1717
this.options = options
18+
this.$body = $(document.body)
1819
this.$element = $(element)
1920
this.$backdrop =
2021
this.isShown = null
@@ -48,6 +49,9 @@
4849

4950
this.isShown = true
5051

52+
this.$body.addClass('modal-open')
53+
54+
this.setScrollbar()
5155
this.escape()
5256

5357
this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
@@ -56,7 +60,7 @@
5660
var transition = $.support.transition && that.$element.hasClass('fade')
5761

5862
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
6064
}
6165

6266
that.$element
@@ -96,6 +100,9 @@
96100

97101
this.isShown = false
98102

103+
this.$body.removeClass('modal-open')
104+
105+
this.resetScrollbar()
99106
this.escape()
100107

101108
$(document).off('focusin.bs.modal')
@@ -153,7 +160,7 @@
153160
var doAnimate = $.support.transition && animate
154161

155162
this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
156-
.appendTo(document.body)
163+
.appendTo(this.$body)
157164

158165
this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
159166
if (e.target !== e.currentTarget) return
@@ -188,6 +195,26 @@
188195
}
189196
}
190197

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+
191218

192219
// MODAL PLUGIN DEFINITION
193220
// =======================
@@ -236,8 +263,4 @@
236263
})
237264
})
238265

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-
243266
}(jQuery);

less/modals.less

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@
118118
}
119119
}
120120

121+
// Measure scrollbar width for padding body during modal show/hide
122+
.modal-scrollbar-measure {
123+
position: absolute;
124+
top: -9999px;
125+
width: 50px;
126+
height: 50px;
127+
overflow: scroll;
128+
}
129+
121130
// Scale up the modal
122131
@media (min-width: @screen-sm-min) {
123132
// Automatically set modal's width for larger viewports

0 commit comments

Comments
 (0)