Skip to content

Commit

Permalink
Fixed #3, added scrollbar width compensation to avoid content moving …
Browse files Browse the repository at this point in the history
…when modal overlay is displayed
  • Loading branch information
FlyingDR committed Feb 21, 2014
1 parent 7834f94 commit f877e57
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
54 changes: 42 additions & 12 deletions jquery.the-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var pluginNamespace = 'the-modal',
// global defaults
defaults = {
lockClass: 'themodal-lock',
overlayClass: 'themodal-overlay',

closeOnEsc: true,
Expand All @@ -22,16 +23,45 @@

cloning: true
};

function lockContainer() {
$('html,body').addClass('lock');
}

function unlockContainer() {
$('html,body').removeClass('lock');
}

function init(els, options) {
var oMargin = {};
var ieBodyTopMargin = 0;

function isIE() {
return ((navigator.appName == 'Microsoft Internet Explorer') ||
(navigator.userAgent.match(/MSIE\s+\d+\.\d+/)) ||
(navigator.userAgent.match(/Trident\/\d+\.\d+/)));
}

function lockContainer(options) {
var tags = $('html, body');

This comment has been minimized.

Copy link
@SukhikhN

SukhikhN Jun 13, 2014

Use of both tags causes margin doubling. Why not set right margin only on html or body?

This comment has been minimized.

Copy link
@FlyingDR

FlyingDR Jun 13, 2014

Author Contributor

Thanks for reporting, I've created pull request that fixes this problem

tags.each(function () {
var $this = $(this);
oMargin[$this.prop('tagName')] = parseInt($this.css('margin-right'));
});
var body = $('body');
var oWidth = body.outerWidth(true);
body.addClass(options.lockClass);
var sbWidth = body.outerWidth(true) - oWidth;
if (isIE()) {
ieBodyTopMargin = body.css('margin-top');
body.css('margin-top', 0);
}
tags.each(function () {
$(this).css('margin-right', oMargin[$(this).prop('tagName')] + sbWidth);
});
}

function unlockContainer(options) {
$('html, body').each(function () {
var $this = $(this);
$this.css('margin-right', oMargin[$this.prop('tagName')]).removeClass(options.lockClass);
});
if (isIE()) {
$('body').css('margin-top', ieBodyTopMargin);
}
}

function init(els, options) {
var modalOptions = options;

if(els.length) {
Expand All @@ -53,7 +83,7 @@
$.modal().close();
}

lockContainer();
lockContainer(localOptions);

var overlay = $('<div/>').addClass(localOptions.overlayClass).prependTo('body');
overlay.data(pluginNamespace+'.options', options);
Expand Down Expand Up @@ -110,7 +140,7 @@
}

overlay.remove();
unlockContainer();
unlockContainer(localOptions);

if(localOptions.closeOnEsc) {
$(document).unbind('keyup.'+pluginNamespace);
Expand Down
4 changes: 2 additions & 2 deletions the-modal.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.lock {
.themodal-lock {
/* when modal is opened we're removing scrollbars from the main content */
overflow: hidden;
}
Expand Down Expand Up @@ -30,4 +30,4 @@
/* IE6–IE8 */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = #7F000000, endColorstr = #7F000000);
zoom: 1;
}
}

0 comments on commit f877e57

Please sign in to comment.