使用 FancyBox 加载内联内容
好吧,我写这篇文章只是希望能帮助其他可能遇到同样问题的人。
供应商网站上的示例有点模糊,我假设了以下场景。
您有一个带有 href
n 的链接,指向某些内容的 #id
。
<a href="#content-div" class="fancybox">Open Example</a>
并且您有一个 div 来保存该内容。 strong>
<div id="content-div" style="display: none">Some content here</div>
然后您只需通过 1-liner 运行 Fancybox。
$(".fancybox").fancybox();
自然,您会认为 Fancybox 会复制内容并更改 display: none
到 display: block
一切都会好起来的。
但这并没有发生。
它仍然加载内容,但内容被隐藏,并且您有一个空白的 Fancybox。 *哭泣*
Well, I'm simply writing this post to hopefully help others that might come across the same issue.
The examples on the vendor website are a little vague and I had assumed the following scenario.
You have a link with a href
n to some content's #id
.
<a href="#content-div" class="fancybox">Open Example</a>
And you have a div to hold that content.
<div id="content-div" style="display: none">Some content here</div>
Then you simply run Fancybox through a 1-liner.
$(".fancybox").fancybox();
Naturally, you'd think that Fancybox will copy the content and change display: none
to display: block
and everything will be ok.
But this doesn't happen.
It still loads the content but the content is hidden and you have a blank Fancybox. *cry*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案非常简单,但我花了大约 2 个小时半的时间才找到它。
只需将您的内容包裹 (冗余)
div
具有display: none
并且 Bob 是你的叔叔。瞧
The solution is very simple, but took me about 2 hours and half the hair on my head to find it.
Simply wrap your content with a (redundant)
div
that hasdisplay: none
and Bob is your uncle.Voila
我解决这个问题的方法是查看 Fancybox 安装附带的示例 index.html/style.css。
如果您查看用于演示网站的代码并基本上复制/粘贴,那就没问题了。
要使内联 Fancybox 工作,您需要在您的 index.html 文件中包含此代码:
请记住准确说明您的脚本文件所在的文件夹以及您在 Head 标记中指向的位置;它们必须对应。
The way I figured this out was going through the example index.html/style.css that comes packaged with the Fancybox installation.
If you view the code that is used for the demo website and basically copy/paste, you'll be fine.
To get an inline Fancybox working, you will need to have this code present in your index.html file:
Remember to be precise about what folders your script files are placed in and where you are pointing to in the Head tag; they must correspond.
只是我为 WordPress 用户发现的东西,
听起来很明显,如果您的 div 返回一些基于通常会链接到新帖子页面的标头的 AJAX 内容,一些教程会说返回 false,因为您正在返回在同一页面上发布数据和返回会阻止页面移动。然而,如果你返回 false,你也会阻止 Fancybox2 做它的事情。我花了好几个小时试图弄清楚这个愚蠢而简单的事情。
因此,对于此类链接,只需确保 href 属性是您希望选择的散列 (#) div,并在您的 javascript 中确保您不会返回 false,因为您不再需要这样做。
简单我知道^_^
Just something I found for Wordpress users,
As obvious as it sounds, If your div is returning some AJAX content based on say a header that would commonly link out to a new post page, some tutorials will say to return false since you're returning the post data on the same page and the return would prevent the page from moving. However if you return false, you also prevent Fancybox2 from doing it's thing as well. I spent hours trying to figure that stupid simple thing out.
So for these kind of links, just make sure that the href property is the hashed (#) div you wish to select, and in your javascript, make sure that you do not return false since you no longer will need to.
Simple I know ^_^