[英]Disable scroll on body when modal view open
I found a JQuery solution to disable scrolling on body when modal view is opened but I am looking for a Javascript solution, can anyone tell me the conversion to JS how it will be : 我找到了一个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()
})})
The best i can come up with vanilla js is this: 我能拿出香草js的最好的是:
http://jsfiddle.net/ngwhk/23/ 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%; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.