HTML - 隐藏iframe

时间:2014-08-21 20:10:04

标签: html iframe

如何隐藏iframe,但仍然加载它?等等。播放youtube歌曲。

<iframe src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1" style="display: none;"></iframe>

这会隐藏iframe,但是它似乎加载了iframe。 (宋没有上演。)

由于

9 个答案:

答案 0 :(得分:11)

使用可以使用宽度0,高度0和边框0

如此更新的代码将是!

<iframe src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1" style="width:0;height:0;border:0; border:none;"></iframe>

<强>加了:

style="width:0; height:0; border:0; border:none"

它会隐藏它,让媒体在后台播放,它也会使它崩溃!

更新小提琴

http://jsfiddle.net/0g51po9t/1/

答案 1 :(得分:8)

我仍然遇到iframe占用空间的问题,直到我添加绝对定位。然后它一起停止占用空间。

<iframe src="/auth/sso/" style="width: 0; height: 0; border: 0; border: none; position: absolute;"></iframe>

答案 2 :(得分:2)

将可见性设置为隐藏。然而,它占用的空间不会崩溃。

<iframe src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1" style="visibility: hidden;"></iframe>

答案 3 :(得分:1)

通过组合这两个答案,我使用height:0;visibility:0,因为这样可以隐藏任何边框或框阴影。

答案 4 :(得分:0)

只需设置:style =“ display:none;”

<iframe src="your url" style="display: none;"></iframe> 

答案 5 :(得分:0)

使用CSS的现有答案对我不起作用。即使使用display:none,我最终还是在iframe所在的位置获得了4x4的点。我要做的是以下事情:

<iframe width="0" height="0" frameborder="0" src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1"></iframe> 

答案 6 :(得分:0)

您可以使用CSS将iframe定位在页面之外,并且不会妨碍页面的放置。这不会在框架上留下点或任何痕迹。

<iframe style="position: absolute; left: -900000px" src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1"></iframe> 

答案 7 :(得分:0)

从iframe删除src,然后将其重新添加到-强制加载iframe:

 /**
 * Issue: iframe doesn't show because it's in a div that is display:none on load
 *
 * Solution: When the button is clicked to show the hidden div:
 *           in the iframe, copy the url from the src attribute
 *           then set the src attribute to ""
 *           then set the src attribute back to its original value.
 *           This forces the iframe to load.
 */

$('#show-div-button').click(function() {

    var iframe = $('iframe');
    var iframe_src = iframe.attr('src');

    iframe.attr('src', '');
    iframe.attr('src', iframe_src);
});

答案 8 :(得分:-1)

iframe {
    visibility: hidden;
    position: absolute;
    left: 0; top: 0;
    height:0; width:0;
    border: none;
}

https://www.dyn-web.com/tutorials/iframes/hidden/