全屏iframe高度为100%
所有浏览器都支持 iframe height=100% 吗?
我使用 doctype 作为:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
在我的 iframe 代码中,如果我说:
<iframe src="xyz.pdf" width="100%" height="100%" />
我的意思是它实际上会占用剩余页面的高度(因为顶部有另一个框架,固定高度为 50px) 所有主要浏览器(IE/Firefox/Safari)都支持此功能吗?
另外关于滚动条,即使我说 scrolling="no"
,我仍然可以在 Firefox 中看到禁用的滚动条...如何完全隐藏滚动条并自动设置 iframe 高度?
Is iframe height=100% supported in all browsers?
I am using doctype as:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
In my iframe code, if I say:
<iframe src="xyz.pdf" width="100%" height="100%" />
I mean will it actually take the height of the remaining page (as there is another frame on top with fixed height of 50px)
Is this supported in all major browsers (IE/Firefox/Safari) ?
Also regarding scrollbars, even though I say scrolling="no"
, I can see disabled scrollbars in Firefox...How do I completely hide scrollbars and set the iframe height automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
您可以使用框架集作为前面的答案状态,但如果您坚持使用 iFrame,则以下 2 个示例应该有效:
替代方案:
使用 2 个替代方案隐藏滚动,如上所示:
使用第二个示例进行黑客攻击:
隐藏滚动条iFrame 的父级被设置为
overflow:hidden
来隐藏滚动条,并且 iFrame 被设置为达到 150% 的宽度和高度,这会强制滚动条在页面之外,因为主体不会有滚动条的人可能不会想到 iframe 会超出页面的边界。这会隐藏全宽 iFrame 的滚动条!You could use frameset as the previous answer states but if you are insistent on using iFrames, the 2 following examples should work:
An alternative:
To hide scrolling with 2 alternatives as shown above:
Hack with the second example:
To hide the scroll-bars of the iFrame, the parent is made
overflow: hidden
to hide scrollbars and the iFrame is made to go upto 150% width and height which forces the scroll-bars outside the page and since the body doesn't have scroll-bars one may not expect the iframe to be exceeding the bounds of the page. This hides the scrollbars of the iFrame with full width!创建全屏
iframe
的 3 种方法:方法 1 - 视口百分比长度
在支持的浏览器中,您可以使用视口百分比长度,例如
高度:100vh
。其中
100vh
表示视口的高度,同样100vw
表示宽度。此处示例
方法 2 - 固定定位
这种方法相当简单。只需设置
fixed
元素的位置并添加height
/width
为100%
。此处示例
方法 3
对于最后一个方法,只需将
body
/html
/iframe
元素的height
设置为 <代码>100%。此处示例
3 approaches for creating a fullscreen
iframe
:Approach 1 - Viewport-percentage lengths
In supported browsers, you can use viewport-percentage lengths such as
height: 100vh
.Where
100vh
represents the height of the viewport, and likewise100vw
represents the width.Example Here
Approach 2 - Fixed positioning
This approach is fairly straight-forward. Just set the positioning of the
fixed
element and add aheight
/width
of100%
.Example Here
Approach 3
For this last method, just set the
height
of thebody
/html
/iframe
elements to100%
.Example Here
我遇到了同样的问题,我正在将 iframe 拉入 div 中。试试这个:
高度设置为 100vh,代表视口高度。另外,宽度可以设置为 100vw,尽管如果源文件具有响应能力,您可能会遇到问题(您的框架将变得非常宽)。
I ran into the same problem, I was pulling an iframe into a div. Try this:
The height is set to 100vh which stands for viewport height. Also, the width could be set to 100vw, although you'll likely run into problems if the source file is responsive (your frame will become very wide).
1. 将 DOCTYPE 更改为不太严格的内容。不要使用 XHTML;这很愚蠢。只需使用 HTML 5 文档类型就可以了:
2. 您可能需要确保(取决于浏览器)iframe 的父级具有高度。及其父级。及其父级。 ETC:
1. Change your DOCTYPE to something less strict. Don't use XHTML; it's silly. Just use the HTML 5 doctype and you're good:
2. You might need to make sure (depends on the browser) that the iframe's parent has a height. And its parent. And its parent. Etc:
这对我来说非常有效(仅当 iframe 内容来自相同域时):
This worked very nicely for me (only if iframe content comes from the same domain):
要获得 iframe 内没有滚动条的全屏 iframe,请使用以下 css。不需要更多
To get a full screen iframe without a scrollbar inside the iframe use the following css. Nothing more is required
以下测试工作
The following tested working
首先添加 css
这将是 html:
You first add css
This will be the html:
http://embedresponsively.com/
这是一个很棒的资源,而且效果很好,我有几次用过它。创建以下代码....
http://embedresponsively.com/
This is a great resource and has worked very well, the few times I've used it. Creates the following code....
对 Akshit Soota 的回答的补充:
显式设置每个父元素的高度很重要,还有表格和列(如果有)的高度:
Additional to the answer of Akshit Soota:
it is importand to explicitly set the height of each parent element, also of the table and column if any:
这是一个简洁的代码。它确实依赖于 jquery 方法来查找当前窗口高度。加载 iFrame 时,它将 iframe 的高度设置为与当前窗口相同。然后,为了处理页面大小的调整,body 标记有一个 onresize 事件处理程序,每当调整文档大小时,该处理程序都会设置 iframe 的高度。
这是一个工作示例: http://jsbin.com/soqeq/1/
Here is a concise code. It does relies on a jquery method to find the current window height. On load of iFrame it sets the height of the iframe be the same as the current window. Then to handle resizing of the page, the body tag has an onresize event handler which sets the iframe's height whenever the document is resized.
here's a working sample: http://jsbin.com/soqeq/1/
如果您的父
使用此处提到的多重解决方案填充高度。
然后你也可以使用类似的东西
if your parent
<div>
fill the height with the multiple solution mentionned here.Then you could also use something like
另一种构建流畅的全屏
iframe
的方法:页面加载时嵌入视频填充整个
viewport
区域对于包含视频或任何嵌入内容的登陆页面来说,这是一个很好的方法。您可以在嵌入视频下方添加任何其他内容,向下滚动页面时会显示这些内容。
示例:
CSS 和 HTML 代码。
Another way to build fluid full screen
iframe
:Embedded video fills entire
viewport
area when page loadsNice approach for landing pages with video or any embedded content. You can have any additional content below of embedded video, which is revealed when scrolling page down.
Example:
CSS and HTML code.
只有这对我有用(但对于“同域”):
您可以使用:
或
Only this worked for me (but for "same-domain"):
you can use either:
or
根据 https://developer.mozilla.org/en- US/docs/Web/HTML/Element/iframe,不再允许使用百分比值。
但以下内容对我有用
虽然
width:100%
有效,但height:100%
不起作用。因此使用了window.innerHeight
。您还可以使用 css 像素作为高度。工作代码: 链接 工作站点:
链接
As per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe, percentage values are no longer allowed.
But the following worked for me
Though
width:100%
works,height:100%
does not work. Sowindow.innerHeight
has been used. You can also use css pixels for height.Working code: Link Working site:
Link
您可以调用一个函数,该函数将在加载 iframe 时计算 iframe 的主体高度:
You can can call a function which will calculate the iframe's body hieght when the iframe is loaded:
使用 jquery 尝试一下,
Try this using jquery,