2014-03-02 30 views
0

我发现了一个JQuery解决方案来禁用模式视图打开时滚动身体,但我正在寻找一个Javascript解决方案,任何人都可以告诉我转换到JS它将如何:当模态视图打开时禁用滚动身体

$(function() { 
var $body = $(window.document.body); 

function bodyFreezeScroll() { 
    var bodyWidth = $body.innerWidth(); 
    $body.css('overflow', 'hidden'); 
    $body.css('marginRight', ($body.css('marginRight') ? '+=' : '') + ($body.innerWidth() - bodyWidth)) 
} 

function bodyUnfreezeScroll() { 
    var bodyWidth = $body.innerWidth(); 
    $body.css('marginRight', '-=' + (bodyWidth - $body.innerWidth())) 
    $body.css('overflow', 'auto'); 
} 

$('.modal').hide() 

$('.longcontent.main button').click(function() { 
    $('.modal').show() 
    bodyFreezeScroll() 
}) 
$('.modal button').click(function() { 
    $('.modal').hide() 
    bodyUnfreezeScroll() 
})}) 

http://jsfiddle.net/ngwhk/

+0

你想把什么部分翻译成香草js?诀窍是拥有一个与当前视口一样大的“背景”容器(top:0; right:0; bottom:0; left:0;)该容器具有css属性overflow:auto(或scroll )所以内容越大,视口就可以滚动。然后,只要dialg打开,您就可以将其应用于body:'document.body.style.overflow =“hidden”;' –

+0

是的,禁用滚动并使其恢复效果很好,但有一个动画的隐藏和显示滚动条,宽度将改变我想要得到这行的等价物:'$ body.css('marginRight',($ body.css('marginRight')?'+ =':' ')+($ body.innerWidth() - bodyWidth))' – Dev4life

+0

我在你的jsfiddle中看不到动画。这只会为身体增加一个边距。 –

回答

0

我能拿出香草JS的最好的是这样的:

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); 
}; 

更新
我发现测量从答案在这里滚动条宽度的方法:How can I get the browser's scrollbar sizes?

由于过渡似乎并没有被触发,如果当我改变风格或添加类(请纠正这一点,你知道一种方式)我为此添加了关键帧动画。

@keyframes show { 
    0% { top: -100%; } 
    100% { top: 0; } 
} 

@keyframes hide { 
    0% { top: 0%; } 
    100% { top: -100%; } 
} 
+0

感谢您的努力,但我们可以防止两个滚动条在转换动画中一起显示 – Dev4life

+0

找到了一种获取当前滚动条的正确大小的方法,并更新了小提琴+答案。参见参考。 –

相关问题