我如何隐藏一个iframe,但仍然加载它?例如:播放 YouTube 歌曲。
<iframe src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1" style="display: none;"></iframe>
这将隐藏iframe,但似乎没有加载iframe。(歌曲没有播放。)
谢谢
我如何隐藏一个iframe,但仍然加载它?例如:播放 YouTube 歌曲。
<iframe src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1" style="display: none;"></iframe>
这将隐藏iframe,但似乎没有加载iframe。(歌曲没有播放。)
谢谢
你可以使用 width: 0; height: 0; position: absolute; 和 border: 0; 来实现。
所以更新后的代码将是这样的!
<iframe src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1" style="position: absolute;width:0;height:0;border:0;"></iframe>
新增:
style="position: absolute; width:0; height:0; border:0;"
它将隐藏该元素,使媒体在后台播放,并且还会折叠它所占用的空间!
更新 Fiddle:
border: none
,你就不需要 border: 0
,因为 0
会被 none
覆盖。 - jcubic在添加了绝对定位之前,我仍然无法解决 iframe 占用空间的问题。添加了绝对定位后,它完全不再占用空间。
<iframe src="/auth/sso/" style="width: 0; height: 0; border: 0; border: none; position: absolute;"></iframe>
将可见性设置为隐藏。但是它占用的空间不会折叠。
<iframe src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1" style="visibility: hidden;"></iframe>
display:none
,我最终还是得到了一个4x4点的iframe位置。我需要做的是以下内容:<iframe width="0" height="0" frameborder="0" src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1"></iframe>
height:0;
和visibility:0
,因为这可以隐藏任何边框或者盒子阴影。只需设置style
为"display: none;"
。
<iframe src="your url" style="display: none;"></iframe>
您可以使用CSS将iframe定位到页面之外,这样就不会留下任何痕迹。
<iframe style="position: absolute; left: -900000px" src="https://www.youtube.com/embed/X18mUlDddCc?autoplay=1"></iframe>
从 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);
});