简体   繁体   English

在fancybox 2上的弹出窗口中显示上一个和下一个图像

[英]Display previous and next image inside popup on fancybox 2

I want to create a layout like this link fancybox layout .我想创建一个这样的布局链接fancybox layout The fancybox provides option to create a tpl, so far so good. Fancybox 提供了创建 tpl 的选项,到目前为止一切顺利。

The problem is that I don't know how to get the next & previous image of the current object and pass it to a div.问题是我不知道如何获取当前对象的下一个和上一个图像并将其传递给 div。

I saw the callbacks listed in fancybox page http://fancyapps.com/fancybox/ but I can't figure out how to do it.我看到了fancybox页面http://fancyapps.com/fancybox/中列出的回调,但我不知道如何去做。 Also I saw this fiddle jsfiddle.net/xW5gs/.我也看到了这个小提琴 jsfiddle.net/xW5gs/。 It stores the previous image link but I was unable to pass it as a background to a div with jquery.它存储了以前的图像链接,但我无法将它作为背景传递给带有 jquery 的 div。

Any help will be appreciated.任何帮助将不胜感激。 Thanks谢谢

Edit:编辑:

I am using fancybox like this:我使用的是这样的fancybox:

$('.fancybox').fancybox({
        'nextEffect': 'none',
        'prevEffect': 'none',
        'tpl': {
            wrap: '<div class="fancybox-wrap" tabIndex="-1"><div id="prev-img"></div><div id="next-img"></div><a title="Close" class="fancybox-item fancybox-close" href="javascript:;"></a><div class="fancybox-skin"><div class="fancybox-outer"><div class="fancybox-inner"></div></div></div></div>',
            closeBtn : '<a title="Close" class="fancybox-item fancybox-close hide-me" href="javascript:;"></a>'
        },
        afterLoad: function(current, previous) {
            console.log( 'Current: ' + current.href );        
            console.log( 'Previous: ' + (previous ? previous.href : '-') );


            if (previous) {
                document.getElementById('prev-img').style.backgroundImage = 'url('+previous.href+')';// here I am trying to pass as a background the url of the previous object to the div with id #prev-img.
                console.log( 'Navigating: ' + (current.index > previous.index ? 'right' : 'left') );     
            }
        }
    });
$(function(){
    $('.fancybox').fancybox({
        'padding' : 0,
        'arrows': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 1000,
        'speedOut': 700,
        'helpers' : { 
            'title' : { type : 'inside',position : 'top'}
        },
        'afterShow': function () {
        // initialize some variables
        var gallerySize = this.group.length,
            next, prev,next_i,prev_i;
        if (this.index == gallerySize - 1) {
            // this is the last element of the gallery so next is the first
            next = $(".fancybox").eq(0).attr("title"),
            prev = $(".fancybox").eq(this.index - 1).attr("title");

            next_i = $(".fancybox").eq(0).attr("href"),
            prev_i = $(".fancybox").eq(this.index - 1).attr("href");
        } else if (this.index == 0) {
            // this is the first image of the gallery so prev is the last
            next = $(".fancybox").eq(this.index + 1).attr("title"),
            prev = $(".fancybox").eq(gallerySize - 1).attr("title");

            next_i = $(".fancybox").eq(this.index + 1).attr("href"),
            prev_i = $(".fancybox").eq(gallerySize - 1).attr("href");
        } else {
            // otherwise just add or substract to index
            next = $(".fancybox").eq(this.index + 1).attr("title"),
            prev = $(".fancybox").eq(this.index - 1).attr("title");

            next_i = $(".fancybox").eq(this.index + 1).attr("href"),
            prev_i = $(".fancybox").eq(gallerySize - 1).attr("href");
        }
        // set title attributes to fancybox next/prev selectors
        $(".fancybox-next").attr("title", next);
        $(".fancybox-prev").attr("title", prev);

        $(".fancybox-next").css('background-image', 'url(' + next_i + ')');
        $(".fancybox-prev").css('background-image', 'url(' + prev_i + ')');
        $(".fancybox-next").css('background-size', 'cover');
        $(".fancybox-prev").css('background-size', 'cover');

    }

    });
 })

You can use this code to display image on next and previous buttons.您可以使用此代码在下一个和上一个按钮上显示图像。

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

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