fancybox - how can I put a link from the opened image?

本秂侑毒 提交于 2019-12-12 08:34:13

问题


-largeAny Ideas folks? I'm trying to link the opened image in fancybox. I've looked everywhere! It sounds so simple...

So here's the code I'm using:

<a id="manual1" href="javascript:;"><img src="/example-thumb.png" alt="example" /></a>
<script type="text/javascript" src="/Cms_Data/Sites/Base/Files/js/fancyboxV2.1.0/jquery.fancybox.pack.js"></script>

<script type="text/javascript">

$("#manual1").click(function() {
$.fancybox([
    '/example-large.jpg',
    '/example-large2.jpg',
    {
        'href'  : '/example-large3.jpg',
        'title' : 'Lorem ipsum '
    }
    ], {
        padding : 38,
        nextEffect : 'fade',
        prevEffect : 'fade'
    });
});
</script>

回答1:


Im still not sure what you are after but is if when you click on the anchor you could do two things: etiher you find the image and its src and replace -thumb to -full and use that to trigger your fancybox method, or you could use html5 data attribute and tell what image url you want:

<a id="manual1" data-image="/example-full.jpg,/example-full-2.jpg'><img src="/example-thumb.png" alt="example" /></a>

<script type="text/javascript">
$('#manual1').click(function() {
    var data = $(this).data('images').split(','),
        options = {
            padding : 38,
            nextEffect : 'fade',
            prevEffect : 'fade',
            type: 'image'
        };
    $.fancybox.open(data , options );
})
 </script>

and a demo: http://jsfiddle.net/voigtan/jJpAM/2/

Demo if you are using one image only

$('.test').click(function() {
    var a = this,
        images = [],
        data = $(a).data('images').split(','),
        options = {
            padding : 38,
            nextEffect : 'fade',
            prevEffect : 'fade',
            type: 'image',
            afterShow: function() {
                $("img.fancybox-image").click(function() {
                    window.location.href = a.href;                        
                });
            }
        };
    $.fancybox.open(data , options );
    return false;
})

and another demo: http://jsfiddle.net/voigtan/jJpAM/3/




回答2:


what I'm after is when the <a id="manual1"> is clicked the example-large is 
displayed in fancybox - the viewed example-large.jpg can then be clicked 
to go to a new page

Another simpler and cleaner approach, based on your code (the fancybox manual method) is to add a custom link option for each image, then use the beforeShow callback to wrap the image with an <a> and its corresponding link like :

$(document).ready(function() {
    $("#manual1").click(function() {
        $.fancybox.open([
            {
            href: 'http://fancyapps.com/fancybox/demo/1_b.jpg',
            title: 'this image links to bbc news',
            link: 'http://www.bbc.co.uk/news/'},
        {
            href: 'http://fancyapps.com/fancybox/demo/2_b.jpg',
            title: 'this image links to jquery',
            link: 'http://jquery.com'},
        {
            href: 'http://fancyapps.com/fancybox/demo/3_b.jpg',
            title: 'this image links to fancybox',
            link: 'http://fancyapps.com'}
        ], {
            beforeShow: function() {
                $(".fancybox-image").wrap('<a href="' + this.link + '" />')
            },
            padding: 38,
            nextEffect: 'fade',
            prevEffect: 'fade'
        });
        return false;
    });
}); // ready

In this way you don't need to pass a long data- attribute in your html (no split needed either) and keep it as simple as :

<a id="manual1" href="javascript:;"><img src="/example-thumb.png" alt="example" /></a>

See DEMO

NOTICE that in my demo, I changed the css properties of the navigation arrows to avoid overlapping with the linked image.




回答3:


this more simple and short code

$.fancybox.open([
{
    href : 'http://fancyapps.com/fancybox/demo/1_b.jpg',
    link : 'http://jquery.com/'}
],{
    afterShow: function() {
    $(".fancybox-image").wrap('<a href="' + this.link + '" />')
        },   

    padding : 0   
});


来源:https://stackoverflow.com/questions/13953018/fancybox-how-can-i-put-a-link-from-the-opened-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!