简体   繁体   English

带有溢出-x 内容的 iframe 的水平滚动条:隐藏

[英]Horizontal scroll bar for iframe with content that has overflow-x: hidden

I am working on a landing page with width 760px and need to have an iframe with content (a flash demo) that is 980px wide.我正在处理一个宽度为 760 像素的登录页面,并且需要一个包含 980 像素宽度的内容(Flash 演示)的 iframe。 So I would need to have a horizontal scrollbar in order to view the entire content.所以我需要有一个水平滚动条才能查看整个内容。 However, no matter what I add as scrolling attributes (eg scrolling="auto/yes" etc), nothing happens - no horizontal scrollbar at all.但是,无论我添加什么滚动属性(例如 scrolling="auto/yes" 等),都不会发生任何事情 - 根本没有水平滚动条。

The page displayed in the iframe has the following command in the source code: iframe中显示的页面在源码中有如下命令:

body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    overflow-x: hidden;
}

As far as I understand it, this is the reason why there is no horizontal scrollbar in my iframe.据我了解,这就是我的 iframe 中没有水平滚动条的原因。 Is there any workaround for that to get one?有没有办法解决这个问题?

You should wrap the iframe in a containing div , applying the fixed-width to the container.您应该将iframe包装在包含div ,将固定宽度应用于容器。

- ——

HTML HTML

<div class="container">
    <iframe></iframe>
</div>

- ——

CSS CSS

.container {
    width: 980px;
    height: auto;
    overflow: scroll;
    -webkit-overflow-scrolling: touch;
}

Try changing the iFrames (body/child) overflow with javascript.尝试使用 javascript 更改 iFrames (body/child) 溢出。

Something like:就像是:

window.onload=function(){
 document.getElementById(one).setStyles({ 'overflow': 'auto' });
};

maybe this, not sure which will get the body of the iframes contant:也许这个,不确定哪个会得到 iframes 的正文:

window.onload=function(){
    var frame = document.getElementById('one'),
    frameDoc = frame.contentDocument || frame.contentWindow.document;
    frameDoc.setStyles({ 'overflow': 'auto' });
};

Mention the width and height in to the iframe在 iframe 中提及宽度和高度

and add overflow-y:hidden; overflow-x:scroll并添加overflow-y:hidden; overflow-x:scroll overflow-y:hidden; overflow-x:scroll in the style of iframe overflow-y:hidden; overflow-x:scroll iframe 样式的overflow-y:hidden; overflow-x:scroll

<iframe width="300" height="315" src="http://webstarts.com" frameborder="0" style="overflow-y:hidden; overflow-x:scroll;"></iframe>

here is the jsFiddle File这是jsFiddle 文件

Following Sergio's answer here is what I did when I found myself with the same problem, using jQuery:按照塞尔吉奥的回答,当我发现自己遇到同样的问题时,我使用 jQuery 做了什么:

  • Take the iframe获取 iframe
  • Search for the body寻找尸体
  • Apply a different overflow property应用不同的溢出属性

$('#iframe').contents().find('body').css('overflow', 'auto');

All this once the page has been completely loaded, of course:一旦页面完全加载,所有这一切,当然:

$(document).ready(function() {...});

Now it displays the scrollbars as I expected现在它按照我的预期显示滚动条

You should use overflow-x: visible.您应该使用溢出-x:可见。 If your content is hosted elsewhere you will not be able to change due to cross domain issues.如果您的内容托管在其他地方,由于跨域问题,您将无法更改。

Imagine if you own a website and someone else wants to iFrame your website and make changes to it ( if you could change the overflow, you could literally change any styling and that would be a huge security leak ).想象一下,如果您拥有一个网站,而其他人想要 iFrame 您的网站并对其进行更改(如果您可以更改溢出,您实际上可以更改任何样式,这将是一个巨大的安全漏洞)。 That is the reason why you can't.这就是你不能的原因。

In your CSS-rule for "body", no "width" is specified.在“body”的 CSS 规则中,没有指定“宽度”。 If I understand it correctly if there is no width then such content can not overflow anything horizontally.如果我理解正确,如果没有宽度,那么这样的内容不能水平溢出任何内容。 It can not be "wider" than anything because it has no "width", so it can not overflow it either.它不能比任何东西“更宽”,因为它没有“宽度”,所以它也不能溢出。

So I would try giving a width-attribute to the body of the document displayed inside the iFrame.所以我会尝试为 iFrame 中显示的文档正文提供一个宽度属性。

Naturally this might work in some browsers but not in others.当然,这可能适用于某些浏览器,但不适用于其他浏览器。

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

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