从功能打开fancybox

时间:2011-02-23 17:06:18

标签: javascript jquery 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 - 任何想法我如何解决这个问题?我知道为什么要触发另一个链接,但我希望另一种解决方案是可能的。

11 个答案:

答案 0 :(得分:40)

如果你想在调用javascript函数时打开一个fancybox。也许在您的代码流中而不是单击的结果。这是你如何做到的:

function openFancybox() {
  $.fancybox({
     'autoScale': true,
     'transitionIn': 'elastic',
     'transitionOut': 'elastic',
     'speedIn': 500,
     'speedOut': 300,
     'autoDimensions': true,
     'centerOnScroll': true,
     'href' : '#contentdiv'
  });
}

这会使用“contentdiv”创建框并打开它。

答案 1 :(得分:26)

由于您正在使用jQuery,因此请停止在HTML中绑定事件处理程序,并开始编写不引人注目的JavaScript。

$(document).ready(function ()
{
    function myfunction(me)
    {
        $(me).fancybox({
            'autoScale': true,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'speedIn': 500,
            'speedOut': 300,
            'autoDimensions': true,
            'centerOnScroll': true // remove the trailing comma!!
        }).click();
        // fire the click event after initializing fancybox on this element
        // this should open the fancybox
    }

    // use .one() so that the handler is executed at most once per element
    $('a[href=#modalMine]').one('click', function ()
    {
        myfunction(this);
        return false;
    });
});

但是,我没有特别看到在点击时设置fancybox的原因。你可以这样做:

$(document).ready(function ()
{
    function myfunction()
    {
        // note the use of "this" rather than a function argument
        $(this).fancybox({
            'autoScale': true,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'speedIn': 500,
            'speedOut': 300,
            'autoDimensions': true,
            'centerOnScroll': true
        });
    }

    $('a[href=#modalMine]').each(myfunction);
});

Basic demo (no images) →

答案 2 :(得分:18)

您需要的是:

$.fancybox.open({ .... });

请参阅此处底部的“API方法”部分:

http://fancyapps.com/fancybox/

答案 3 :(得分:8)

您根本不必添加自己的click事件处理程序。只需使用fancybox初始化元素:

$(function() {
    $('a[href="#modalMine"]').fancybox({
        'autoScale': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 500,
        'speedOut': 300,
        'autoDimensions': true,
        'centerOnScroll': true  // as MattBall already said, remove the comma
    });
});

完成。 Fancybox已绑定打开该框的click处理程序。 Have a look at the HowTo section.


稍后,如果您想以编程方式打开该框,请在该元素上引发click事件:

$('a[href="#modalMine"]').click();

答案 4 :(得分:3)

使参数对象从<a>延伸, 并通过委托在点击事件中使用fancybox的open功能。

    var paramsFancy={
         'autoScale': true,
         'transitionIn': 'elastic',
         'transitionOut': 'elastic',
         'speedIn': 500,
         'speedOut': 300,
         'autoDimensions': true,
         'centerOnScroll': true,
         'href' : '#contentdiv'
      };

    $(document).delegate('a[href=#modalMine]','click',function(){
               /*Now you can call your function ,
   you can change fields of paramsFancy via this function */
                 myfunction(this);

                paramsFancy.href=$(this).attr('href');
                $.fancybox.open(paramsFancy);

    });

答案 5 :(得分:3)

您不必触发点击事件,您可以使用fancybox类型作为ajax。

$.fancybox.open({
      href: "http://........",
      type: 'ajax'
});

答案 6 :(得分:2)

答案似乎有点过于复杂。我希望我没有误解这个问题。

如果您只想打开一个奇特的盒子,从点击到“A”标签。只需将您的HTML设置为

即可
<a id="my_fancybox" href="#contentdiv">click me</a>

您的信箱内容将位于ID为“contentdiv”的div中,您可以在javascript中初始化fancybox:

$('#my_fancybox').fancybox({
    'autoScale': true,
    'transitionIn': 'elastic',
    'transitionOut': 'elastic',
    'speedIn': 500,
    'speedOut': 300,
    'autoDimensions': true,
    'centerOnScroll': true,
});

当您点击锚标记时,这将显示包含“contentdiv”的fancybox。

答案 7 :(得分:2)

以下是根据作者Tips & Tricks博客文章的工作代码,将其放入文档准备中:

  $("#mybutton").click(function(){
    $(".fancybox").trigger('click');  
  })

这会触发当前显示的图像或内容的较小版本,就像您手动单击它一样。它避免再次初始化Fancybox,而是保留在文档就绪时初始化它的参数。如果你需要做一些与单独按钮打开盒子不同的东西,而不是点击盒子,你需要参数,但对于很多人来说,这将是他们想要的。

答案 8 :(得分:1)

function myfunction(){
    $('.classname').fancybox().trigger('click'); 
}

对我有用..

答案 9 :(得分:1)

...
<div class="img_file" style="background-image: url('/upload/test/14120330.jpg')" data-img="/upload/test/14120330.jpg"></div>
<div class="img_file" style="background-image: url('/upload/test/14120330.jpg')" data-img="/upload/test/14120330.jpg"></div>
...
if($(".img_file[data-img]").length) {
                var imgs_slider_img = [];
                $(".img_file[data-img]").each(function(i) {
                    imgs_slider_img.push({
                        href:$(this).attr('data-img')
                    });
                    $(this).attr('data-index-img', i);
                }).on("click", function(e) {
                    e.preventDefault();
                    $.fancybox.open(imgs_slider_img, {
                         index: $(this).attr('data-index-img'),
                         padding: 0,
                         margin: 0,
                         prevEffect: 'elastic',
                         nextEffect: 'elastic',
                         maxWidth: '90%',
                         maxHeight: '90%',
                         autoWidth: true,
                         autoHeight: true
                    });
                });
            }
...

答案 10 :(得分:0)

如果jQuery.fancybox.open不可用(在fancybox 1.3.4上),您可能需要使用semafor来解决递归问题:

<a href="/index.html" onclick="return myfunction(this)">click me</a>

<script>

var clickSemafor = false;
myfunction(el)
{
 if (!clickSemafor) {
   clickSemafor = true; 
   return false; // do nothing here when in recursion
 }
 var e = jQuery(el); 
 e.fancybox({
    type: 'iframe',
    href: el.href
  }); 
 e.click();  // you could also use e.trigger('click');
 return false; // prevent default

}
</script>