从函数中打开 fancybox
我正在尝试从我拥有的函数中打开一个 fancybox - 简而言之,我的 HTML 代码如下所示;
<a href="#modalMine" onclick="myfunction(this); return false;">
click
</a>
我的功能的一部分如下所示;
function myfunction(me) {
$(me).fancybox({
'autoScale': true,
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'speedIn': 500,
'speedOut': 300,
'autoDimensions': true,
'centerOnScroll': true,
});
}
上面的内容在 IE 中有效,但在 FireFox 或 Chrome 中无效 - 知道如何解决这个问题吗?我知道原因之一是触发另一个链接,但我希望可以有另一种解决方案。
I am trying to open a fancybox from a function I have - in short my HTML code looks like this;
<a href="#modalMine" onclick="myfunction(this); return false;">
click
</a>
and a part of my function looks like this;
function myfunction(me) {
$(me).fancybox({
'autoScale': true,
'transitionIn': 'elastic',
'transitionOut': 'elastic',
'speedIn': 500,
'speedOut': 300,
'autoDimensions': true,
'centerOnScroll': true,
});
}
The above works in IE but not in FireFox or Chrome - any idea how I can fix this? I know that one why is to trigger another link, but I hope another solution is possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您想在调用 javascript 函数时简单地打开一个 fancybox。也许在您的代码流中而不是单击的结果。操作方法如下:
这将使用“contentdiv”创建框并打开它。
If you'd like to simply open a fancybox when a javascript function is called. Perhaps in your code flow and not as a result of a click. Here's how you do it:
This creates the box using "contentdiv" and opens it.
由于您使用的是 jQuery,因此请停止在 HTML 中绑定事件处理程序,并开始编写不引人注目的 JavaScript。
但是,我没有特别看到在点击时设置 fancybox 的原因。您可以这样做:
基本演示(无图像)→
Since you're using jQuery, stop binding event handlers in your HTML, and start writing unobtrusive JavaScript.
However, I don't particularly see a reason for setting up the fancybox on click. You could just do this instead:
Basic demo (no images) →
您需要的是:
请参阅此处底部的“API 方法”部分:
http://fancyapps.com/fancybox/
What you need is:
See the "API methods" section at the bottom of here:
http://fancyapps.com/fancybox/
您根本不必添加自己的
click
事件处理程序。只需使用 fancybox: Done 初始化元素即可。 Fancybox 已经绑定了一个打开该框的
click
处理程序。 查看“操作方法”部分。稍后,如果您想以编程方式打开该框,请抬起
单击< /code> 该元素上的事件:
You don't have to add you own
click
event handler at all. Just initialize the element with fancybox:Done. Fancybox already binds a
click
handler that opens the box. Have a look at the HowTo section.Later if you want to open the box programmatically, raise the
click
event on that element:您不必触发点击事件,您可以使用 fancybox 类型作为 ajax 来完成。
You do not have to trigger a click event, you can do it with fancybox type as ajax.
使参数对象扩展自
,
并通过委托在点击事件中使用 fancybox 的
open
函数。Make argument object extends from
<a>
,and use
open
function of fancybox in click event via delegate.答案似乎有点过于复杂。我希望我没有误解这个问题。
如果您只是想通过点击“A”标签来打开一个精美的盒子。只需将您的 html 设置为
您的盒子的内容将位于 id 为“contentdiv”的 div 内,并且在您的 javascript 中您可以像这样初始化 fancybox:
单击锚标记时,这将显示一个包含“contentdiv”的 fancybox。
The answers seems a bit over complicated. I hope I didn't misunderstand the question.
If you simply want to open a fancy box from a click to an "A" tag. Just set your html to
The contents of your box will be inside of a div with id "contentdiv" and in your javascript you can initialize fancybox like this:
This will show a fancybox containing "contentdiv" when your anchor tag is clicked.
这是根据作者的 提示和提示的工作代码。技巧博客文章,将其放入文档中:
这会触发当前显示的图像或内容的较小版本,就像您手动单击它一样。它避免再次初始化 Fancybox,而是保留文档中初始化时使用的参数。如果您需要在使用单独的按钮打开该框时与单击该框进行不同的操作,则您将需要参数,但对于许多人来说,这将是他们所寻找的。
Here is working code as per the author's Tips & Tricks blog post, put it in document ready:
This triggers the smaller version of the currently displayed image or content, as if you had clicked on it manually. It avoids initializing the Fancybox again, but instead keeps the parameters you initialized it with on document ready. If you need to do something different when opening the box with a separate button compared to clicking on the box, you will need the parameters, but for many, this will be what they were looking for.
它对我有用..
It works for me..
如果 jQuery.fancybox.open 不可用(在 fancybox 1.3.4 上),您可能需要使用 semafor 来解决递归问题:
if jQuery.fancybox.open is not available (on fancybox 1.3.4) you may need to use semafor to get around the recursion problem: