jQuery: Center fancybox in viewport

martincarlin87 picture martincarlin87 · Aug 22, 2012 · Viewed 15.5k times · Source

I am currently using fancybox version 2 to display my images when a thumbnail is clicked but I can't seem to get the fancybox to sit in the middle of the viewport, vertically speaking, since horizontally it's fine.

I've tried quite a few things and this is the latest:

CSS

.fancybox-wrap { 
    position: fixed !important;
    top: 50px !important;
} 

jQuery

jQuery(document).ready(function() {
    $(".fancybox-wrap").css("position", "absolute");

    // fancybox initialisation etc

});


$(window).scroll(function() {
    $('.fancybox-wrap').css('top', $(this).scrollTop() + "50px");
});

Does anyone have any ideas?

Answer

Marina Anayko picture Marina Anayko · Aug 24, 2017

The only thing you need to do is add to website css

html, body {height: 100%;}

And fancybox will position image at the screen center instead of page center.