The best i can come up with vanilla js is this:
http://jsfiddle.net/ngwhk/23/
var $body = window.document.body;
var $modal = document.getElementsByClassName("modal")[0];
var aOpenButtons = document.getElementsByClassName("open-modal");
var aCloseButtons = document.getElementsByClassName("close-modal");
function bodyFreezeScroll($obj) {
$body.style.overflow = "hidden";
$body.style.marginRight = getScrollBarWidth()+"px";
}
function bodyUnfreezeScroll() {
$body.style.overflow = "auto";
$body.style.marginRight = "auto";
}
function showModal(){
$modal.classList.add("visible");
$modal.classList.remove("hide");
}
function hideModal(){
$modal.classList.add("hide");
$modal.classList.remove("visible");
}
for(var i = 0, a=aOpenButtons.length; i<a; i++){
aOpenButtons[i].addEventListener("click", function(){
showModal();
bodyFreezeScroll();
}, false);
}
for(var i = 0, a=aCloseButtons.length; i<a; i++){
aCloseButtons[i].addEventListener("click", function(){
hideModal();
bodyUnfreezeScroll();
}, false);
}
function getScrollBarWidth() {
/* found here: https://stackoverflow.com/questions/986937/how-can-i-get-the-browsers-scrollbar-sizes */
var inner = document.createElement('p');
inner.style.width = "100%";
inner.style.height = "200px";
var outer = document.createElement('div');
outer.style.position = "absolute";
outer.style.top = "0px";
outer.style.left = "0px";
outer.style.visibility = "hidden";
outer.style.width = "200px";
outer.style.height = "150px";
outer.style.overflow = "hidden";
outer.appendChild (inner);
document.body.appendChild (outer);
var w1 = inner.offsetWidth;
outer.style.overflow = 'scroll';
var w2 = inner.offsetWidth;
if (w1 == w2) w2 = outer.clientWidth;
document.body.removeChild (outer);
return (w1 - w2);
};
Update
I found a method to measure the scrollbar width from the answer here: How can I get the browser's scrollbar sizes?
Since the transitions seems not to be triggered, when i change the style or add classes (please correct this, if you know a way) I added keyframes animations for that.
@keyframes show {
0% { top: -100%; }
100% { top: 0; }
}
@keyframes hide {
0% { top: 0%; }
100% { top: -100%; }
}