简体   繁体   English

从按钮单击启动fancyBox

[英]Launch fancyBox from button click

Im stuck with a little problem with fancyBox v2. 我遇到了fancyBox v2的一个小问题。

I want to launch fancyBox on button click. 我想在按钮点击时启动fancyBox。 Once clicked, it will load all the images from the list (from the src attribute of the ). 点击后,它将加载列表中的所有图像(来自的src属性)。

I have created this jsfiddle to show what I am trying to do: http://jsfiddle.net/fPFZg/ 我创建了这个jsfiddle来展示我想要做的事情: http//jsfiddle.net/fPFZg/

jQuery(document).ready(function($) {
    $('button').on('click', function() {
        $.fancybox(); 
    });
});

Can anybody see how this would be possible? 任何人都可以看到这是可能的吗?

I had the same question and found the following to be a simpler method: 我有同样的问题,发现以下是一个更简单的方法:

HTML HTML

    <button class="fancybox" value="Open Fancybox">Open Fancybox</button>
    <div class="hidden" id="fancybox-popup-form">
        (your Fancybox content goes in here)
    </div>

jQuery jQuery的

    $('.fancybox').click(function () {
        $.fancybox([
            { href : '#fancybox-popup-form' }
        ]);
    });

CSS CSS

    .hidden { display: none; }

Further Reading 进一步阅读

Fancybox Docs (click the "API Methods" tab, then read up on the first method, which is called "Open"). Fancybox文档 (单击“API方法”选项卡,然后阅读第一个方法,称为“打开”)。

you can use it like this: 你可以像这样使用它:

    $.fancybox.open([
    {
        href : 'http://fancyapps.com/fancybox/demo/1_b.jpg',
        title : '1st title'
    },
    {
        href : 'http://fancyapps.com/fancybox/demo/2_b.jpg',
        title : '2nd title'
    }    
], {
    padding : 0   
});

Your code doesn't work because this $.fancybox(); 你的代码不起作用,因为这个$.fancybox(); doesn't tell fancybox what to open so does nothing. 没有告诉fancybox打开什么,所以什么都不做。

What I would do, if you don't want to edit every link in your html, is: 如果您不想编辑html中的每个链接,我会怎么做:

1). 1)。 add an ID to the <ul> tag so we can use that selector like <ul>标签中添加一个ID ,以便我们可以使用该选择器

<ul id="images">

2). 2)。 bind fancybox to all <a> anchors that are child of your element #images like 将fancybox绑定到所有<a>锚点,这些锚点是元素#images子元素

var fancyGallery = $("#images").find("a"); // we cache the selector here
fancyGallery.attr("rel","gallery").fancybox({
    type: "image"
});

notice that we are adding a rel attribute on the fly to all anchors so they will be part of the same gallery 请注意 ,我们正在向所有锚点添加一个rel属性,因此它们将成为同一个库的一部分

3). 3)。 bind a click event to the button (I would recommend you to give it an ID too so it won't mess with other possible buttons in the future) that triggers the existing fancybox script as above, so with this html 将一个click事件绑定到该button (我建议你也给它一个ID以便将来不会弄乱其他可能的按钮 )触发现有的fancybox脚本如上所述,所以使用这个html

<button id="fancyLaunch">Launch Fancybox</button>

use this script 使用这个脚本

$('#fancyLaunch').on('click', function() {
    fancyGallery.eq(0).click(); // triggers a click
});

notice that we used the method .eq() to launch the gallery from the first item (first index in javascript is always 0) 请注意我们使用方法.eq()从第一个项目启动库(javascript中的第一个index始终为0)

Summarizing, your whole script may look like this : 总结一下,您的整个脚本可能如下所示:

jQuery(document).ready(function($) {
    var fancyGallery = $("#images").find("a");
    fancyGallery.attr("rel","gallery").fancybox({
        type: "image"
    });
    $('#fancyLaunch').on('click', function() {
        fancyGallery.eq(0).click(); 
    });
});

See JSFIDDLE 请参阅JSFIDDLE

Your html: 你的HTML:

<ul>
<li>
    <a href="http://placekitten.com/400/400" title="" class="fancybox">
        <img src="http://placekitten.com/100/100" alt="" />
    </a>
</li>

 <li>
    <a href="http://placekitten.com/400/400" title="" class="fancybox">
        <img src="http://placekitten.com/100/100" alt="" />
    </a>
</li>

<li>
    <a href="http://placekitten.com/400/400" title="" class="fancybox">
        <img src="http://placekitten.com/100/100" alt="" />
    </a>
</li>

<li>
    <a href="http://placekitten.com/400/400" title="" class="fancybox">
        <img src="http://placekitten.com/100/100" alt="" />
    </a>
</li>

Your jQuery: 你的jQuery:

 $(document).ready(function() {
$('button').on('click', function() {
    $.fancybox($("a.fancybox")); 
});});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM