fancybox 2: put thumbnails within a parent div

非 Y 不嫁゛ 提交于 2019-12-04 11:40:46

There are not too much documentation about the tpl API option so I will share with you my findings.


1). whats the tpl : {},? : tpl stands for template, and it's a collection of data entries (json name/value pair format) that returns metadata inside the fancybox function.

obj : {
    property-name: value // Boolean (true/false), integer (50, 260.3) or string ("string" ... note the quotes)
}

You can use tpl to add your own selectors to fancybox if you want to add different css styles like :

$(".fancybox").fancybox({
    tpl: {
        wrap: '<div class="fancybox-wrap mywrap" tabIndex="-1"><div class="fancybox-skin"><div class="fancybox-outer"><div class="fancybox-inner"></div></div></div></div>'
    },
});

Notice I added the class mywrap so you can use its selector to add custom css styles (or manipulate it through jQuery) like :

/* add a 5px red border around fancybox */
.mywrap {
    border: solid 5px #880000 !important;
}

See JSFIDDLE


2). Basically all I need is to have a parent div for both the gallery and the thumbnails. This has to be seen from different angles.

When you open fancybox two main containers are added (by selector) :

2.a). .fancybox-overlay

  • it's the semi-transparent overlay on top of your page AND behind fancybox
  • it's always appended to the body

2.b). .fancybox-wrap

  • it's the fancybox box with all its children (navigation and close buttons, content, etc.)
  • it's appended to .fancybox-overlay IF the locked option is set to true (default value) like :

    $(".fancybox").fancybox({
        helpers : {
            overlay: {
                locked: true
            }
        }
    });
    
  • OR it's appended to body IF the locked option is set to false :

    $(".fancybox").fancybox({
        helpers : {
            overlay: {
                locked: false
            }
        }
    });
    

    NOTE: .fancybox-wrap will be a sibling of .fancybox-overlay in this case.

  • it's appended to any designated custom container when using the option parent AND the locked option is set to false

    so having this html:

    <body>
        <div id="mainWrapper"></div>
    </body>
    

    ... and

    $(".fancybox").fancybox({
        parent: "#mainWrapper", // parent div
        helpers : {
            overlay: {
                locked: false
            }
        }
    });
    

    ....fancybox-wrap will be appended as a child element of #mainWrapper so you can apply your own custom css styles like :

    #mainWrapper .fancybox-wrap {
        left: 0 !important;
    }
    

    See JSFIDDLE


3). When you use the thumbnails helper (and linked the proper js and css files), another container is added by fancybox :

3.a). #fancybox-thumbs

  • it's always appended to the body
  • it's a sibling of the .fancybox-overlay element

If you require a parent div for the #fancybox-thumbs container, you could wrap #fancybox-thumbs with its new custom parent div on the fly using a fancybox callback like :

$(".fancybox").fancybox({
    helpers: {
        thumbs: {}
    },
    afterShow: function () {
        if ($(".mythumbswrap").length == 0) {
            setTimeout(function () {
                $("body").find("#fancybox-thumbs").css({
                    // custom css (optional)
                    position: "relative"
                }).wrap("<div class='mythumbswrap' />");
            }, 100);
        }
    },
    afterClose: function () {
        $(".mythumbswrap").remove();
    }
});

Notice that we first checked if .mythumbswrap already existed, which can be true if I am browsing a gallery. If not, we'll add it BUT we will need to wait for #fancybox-thumbs to be appended to the body since it doesn't happen until fancybox is completely added to the DOM.

We used setTimeout to set the delay.

Also notice that we removed the parent container .mythumbswrap after closing fancybox, otherwise it will remain in the DOM and not added as a wrapper of #fancybox-thumbs next time we open fancybox.

Since now .mythumbswrap is the parent element of #fancybox-thumbs we can apply any custom styles like :

.mythumbswrap {
  background: none repeat scroll 0 0 #FFFFFF !important;
  bottom: 0;
  display: block;
  height: 60px;
  width: 100%;
  z-index: 10000;
  position: fixed;
  bottom: 0;
}

See JSFIDDLE

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