Fancybox:加载 iframe 内容时显示加载动画
当使用 jQuery 和 fancybox 在 iframe 内显示另一个页面时,我想在 iframe 内的页面显示加载动画正在加载。当使用 ajax 加载内容时这是可能的,但在使用 iframe 时我想要同样的事情。是否可以在 iframe 中的内容加载时从 api 获取加载动画?
这是在 fancybox 中显示 iframe 的代码:
var $fancybox = $("#openPopup").fancybox({
'type': 'iframe',
'href': "Test.php"
});
When using jQuery and fancybox for showing another page inside an iframe I want to show the loading animation while the page inside the iframe is loading. This is possible when using ajax to load content but I want the same thing when using iframe. Is it possible from the api to also get the loading animation while the content in the iframe is loading?
This is the code for showing the iframe in the fancybox:
var $fancybox = $("#openPopup").fancybox({
'type': 'iframe',
'href': "Test.php"
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
FancyBox 提供了开箱即用的功能。
当您单击按钮或链接打开 iframe 时,Fancybox 会将 div#fancybox_loading 附加到正文,并在 iframe 完全加载后删除该 div。
您只需要确保 div#fancybox-loading 为“display:block”。
图像显示 div#fancybox-loading
FancyBox provides this out-of-the-box.
When you click a button or link to open the iframe,Fancybox will append a div#fancybox_loading to the body, and remove the div once iframe fully loaded.
You just need to make sure the div#fancybox-loading is 'display:block'.
image shows the div#fancybox-loading
no-javascript,为每个浏览器提供反应就绪解决方案支持(html 1.0)
另一种具有相同支持但没有额外请求的方法:
no-javascript, react ready solution support for every browser (html 1.0)
Another approach with same support but no extra request:
您可以简单地将带有 CSS 的背景附加到下面的图层,当 iframe 准备就绪时,它将与微调器重叠您的背景
You can simply attach backgroung with CSS to layer below, that when iframe will be ready it will overlap your backgroung with spinner
这对我有用:
This worked for me:
我也有同样的问题。以下是我在阅读史蒂夫的回答后实施问题解决方案的方法。
HTML:
JavaScript:
当然,这不适用于设置了 X-Frame-Options HTTP 标头的外部 URL。例如 https://www.google.com.au/。
I had the same question. Here's how I implemented a solution to the problem after reading Steve's answer.
HTML:
JavaScript:
Of course this won't work for external URLs with the X-Frame-Options HTTP header set. For example https://www.google.com.au/.
不显示加载动画的原因是,对于此类内容,fancybox 不知道任何加载概念;它只是以指定的尺寸覆盖一个 iframe,一旦完成,就 fancybox 而言,工作就完成了。
然后,iframe 继续加载按照正常浏览器功能指定的内容。
我想您为启用加载程序而必须付出的努力将远远超过拥有它的好处。
无论如何,问题动态插入的iframe中的jQuery .ready应该有所帮助你在路上;它向您展示了如何将回调推送到 iframe 的加载事件中,您可以在单击添加加载微调器后使用该回调来删除加载微调器?
当然,获得对 fancybox 覆盖的 iframe 的实际控制还存在进一步的复杂性,希望 API 能对此有所帮助,但我再次不确定这是否值得付出努力。
The reason the loading animation isn't displayed is because there isn't any concept of loading that fancybox is aware of for this kind of content; it simply overlays an iframe at the specified dimensions and once that's complete as far as fancybox is concerned the job is done.
The iframe then proceeds to load the content specified as per normal browser functionality.
I'd imagine the effort you'll have to put in to enable the loader will far outweigh the benefit of having it.
Regardless, the question jQuery .ready in a dynamically inserted iframe should help you on your way; it shows you how to push a callback into the iframe's load event, which you could use to remove the loading spinner after having added it on click?
There are of course further complications in getting actual control of the iframe that fancybox overlays, hopefully the API would aid this but again I'm unsure that it's worth the effort.
http://panduwana.wordpress.com/2010/05/09/ fancybox-iframe-patch/
这应该有帮助:-)
它是原始 Fancybox(最新版本,BTW)的修改版。
小心!根据我从描述中读到的内容,这不适用于跨域 iFrame。
http://panduwana.wordpress.com/2010/05/09/fancybox-iframe-patch/
THAT should help :-)
It's a modification of the original Fancybox (the latest version, BTW).
Careful! From what I've read from the description, this won't work for cross-domains iFrames.
您可以将处理程序附加到 fancybox iframe 的加载事件并隐藏加载程序。
You can attach a handler to the fancybox iframe's load event and hide the loader.
这篇帖子中有更多答案。不幸的是,我找不到“真正的”解决方案,但其他人正在替换页面覆盖 CSS (#fancybox-overlay) 以包含微调器。然后,当 iframe 页面最终加载时,它会被放置在微调器上。
编辑:
我放弃了使用 fancybox 来处理加载图像,并认为我会分享我的解决方案。我在页面中添加了我自己的绝对定位加载
gif
,在调用 fancybox 之前显示它,我可以使用 JQuery 捕获 iframe 加载事件来隐藏微调器,如下所示:如果您不这样做像我一样手动调用 fancybox,我假设您可以通过单击事件轻松附加此代码,即:
我在玩 fancybox (v2.0.6) 源时也注意到,它看起来像 fancybox 使用加载事件如果 autoSize 与 iFrame 一起使用,则显示/隐藏加载动画,尽管我尚未测试该理论。
There are more answers in this SO post. Unfortunately I cannot find a "real" solution, but other people are replacing the page overlay CSS (#fancybox-overlay) to include a spinner. Then when the iframe'd page eventually loads it's placed over the spinner.
EDIT:
I gave up on using fancybox to handle the loading image and thought I'd share my solution. I added an absolutely positioned loading
gif
of my own to the page, show it just before calling fancybox and I can capture the iframe loaded event with JQuery to hide the spinner, like so:if you're not calling fancybox manually like I am, I assume you could just as easily attach this code through a click event, i.e:
I've also noticed while playing in the fancybox (v2.0.6) source that it looks like fancybox uses the load event to show/hide the loading animation if autoSize is used with an iFrame, though I haven't tested the theory.
Fancybox 2 现已存在,并提供开箱即用的功能。
Fancybox 2 exists now, and provides this out-of-the-box.
这是用 z-index 解决的
This is solve with z-index