简体   繁体   English

身体溢出-y隐藏,onclick

[英]Body overflow-y hidden, onclick

I happily use this simple (on click) function to enable my 100%-100% sized iframe's visibility: 我很高兴使用这个简单的(点击)功能来启用我的100%-100%大小的iframe的可见性:

<script language="javascript">
            function newwindow() {
            var showme = document.getElementById("frame");
            showme.style.visibility = "visible";
            }
</script>

Now.. within the same function, I'd like to disable the vertical scrolling (on the parent page's body). 现在..在同一个函数中, 我想禁用垂直滚动 (在父页面的主体上)。

I googled for hours. 我用谷歌搜索了几个小时。 Stuck! 卡住! I tried stuff like this: 我试过这样的东西:

document.getElementById('myBodyID').style.overflowY='hidden'

(document.getElementById('myBodyID').style["overflow-y"]).

and more, but nothing good happens. 还有更多,但没有什么好事发生。 It looks like the 'overflow'has problems, in fact I made a quick test with this: 看起来'溢出'有问题,事实上我做了一个快速测试:

document.body.style.backgroundColor="yellow"

and it worked correctly, page turned yellow. 它工作正常,页面变黄。 But the same logic is not working with body's 'overflow'. 但同样的逻辑并不适用于身体的“溢出”。

Can someone help? 有人可以帮忙吗? Thanks! 谢谢!

For the code to work as written, your <body> needs to be <body id="myBodyID"> 要使代码按写入方式工作,您的<body>必须是<body id="myBodyID">

The syntax is just 语法就是这样

document.getElementById("myBodyID").style.overflowY = "scroll";

You may have to wait until window load if that script is called in <head> ; 如果在<head>调用该脚本,您可能必须等到窗口加载; body doesn't exist yet. 身体还不存在。 Document ready is technically sufficent but that's non-trivial without jQuery. 文档准备在技术上是足够的,但没有jQuery这是非平凡的。 Alternatively, move the script to the bottom of your page, or just use CSS. 或者,将脚本移动到页面底部,或者只使用CSS。

this works for me 这对我有用

<html>
<head>
<style>
html,body{
margin:0;
padding:0;
}
</style>
<script>
            function newwindow() {
            var showme = document.getElementById("frame");
            showme.style.visibility = "visible";
            document.body.style.overflow="hidden";
            }
</script>
</head>
<body onload="newwindow()">
<iframe id="frame" width="100%" height="100%" src="http://armblog.net">
</body>
<html>

Ok I love you all. 好的,我爱你们。

Listen, I did try all the mentioned options and all of them where correct (thanks). 听着,我确实尝试了所有提到的选项,所有这些都是正确的(谢谢)。

But it was not working in my html, why? 它不能在我的HTML中工作,为什么?

I just found out the reason: In my css I had: body overflow- x : hidden; 我刚刚发现了原因:在我的CSS中我有:身体溢出 - x :隐藏; that was working ok for me and it was looking innocent. 这对我来说还不错,看起来很无辜。

Actually it was preventing the mentioned script to work. 实际上它阻止了上述脚本的工作。 I dunno why. 我不知道为什么。 I removed it just to make a stupid try, now it all works good. 我删除它只是为了做一个愚蠢的尝试,现在一切正常。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM