Javascript 以编程方式更改 body.style.overflowY 或 X

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6059489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:04:23  来源:igfitidea点击:

changing body.style.overflowY or X programmatically

javascriptcss

提问by Phil

I'm trying to change the body overflowYto "hidden"through a JavaScript function. I've tried both of the following:

我想身体改变overflowY"hidden"通过JavaScript函数。我已经尝试了以下两种方法:

document.body.style['overflow-y'] = 'hidden';
document.body.style.overflowY = "hidden";

In both cases it didn't work. However in both cases using just overflowwithout X or Y works fine!

在这两种情况下,它都不起作用。但是,在这两种情况下,只使用overflow不使用X 或 Y 都可以正常工作!

Is there a way to change only the overflowYor overflowXproperties?

有没有办法只改变overflowYoverflowX属性?

回答by Andy E

Both cases should work in all the current browsers. I've just tested Firefox 4, Chrome 11 and IE 9 and they all work fine. Either the browser you're testing with has a bug or your problem lies elsewhere in your code; for instance, a conflicting style declaration or erroneous code in the same block.

这两种情况都应该适用于所有当前浏览器。我刚刚测试了 Firefox 4、Chrome 11 和 IE 9,它们都运行良好。您正在测试的浏览器存在错误,或者您的问题出在代码的其他地方;例如,冲突的样式声明或同一块中的错误代码。

Working example: http://jsfiddle.net/4dq64/

工作示例:http: //jsfiddle.net/4dq64/