Fancybox on iPad/iPhone

Using the Fancybox script on the iPhone and iPad caused the overlay to not entirely fill the screen, which is an ugly problem!

Look for this line of code in the fancybox/jquery.fancybox-VER.js (not the packed file):

if (currentOpts.overlayShow) {
	overlay.css({
			'background-color' : currentOpts.overlayColor,
			'opacity' : currentOpts.overlayOpacity,
			'cursor' : currentOpts.hideOnOverlayClick ? 'pointer' : 'auto',
			'height' : $(document).height()
	});
}

Add a width property:

if (currentOpts.overlayShow) {
	overlay.css({
			'background-color' : currentOpts.overlayColor,
			'opacity' : currentOpts.overlayOpacity,
			'cursor' : currentOpts.hideOnOverlayClick ? 'pointer' : 'auto',
			'height' : $(document).height(),
			'width' : $(document).width()
	});
}

Also replace the viewport code to center the lightbox (the original code is commented):

_get_viewport = function() {
			/*return [
				$(window).width() - (currentOpts.margin * 2),
				$(window).height() - (currentOpts.margin * 2),
				$(document).scrollLeft() + currentOpts.margin,
				$(document).scrollTop() + currentOpts.margin
			];*/
			var uagent = navigator.userAgent.toLowerCase();
			if(uagent.search('ipad') > -1 || uagent.search('iphone') > -1 || uagent.search('ipod') > -1 || uagent.search('android') > -1) {
 
				var viewportwidth;
                var viewportheight;
 
                if (typeof window.innerWidth != 'undefined') {
                	viewportwidth = window.innerWidth;
                	viewportheight = window.innerHeight;
                } else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
                	viewportwidth = document.documentElement.clientWidth;
                	viewportheight = document.documentElement.clientHeight;
                } else {
                	viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
                	viewportheight = document.getElementsByTagName('body')[0].clientHeight;
                }
                return [
                	viewportwidth,
                	viewportheight,
                	$(document).scrollLeft(),
                	$(document).scrollTop()
                ];
            } else {
                return [
                	$(window).width() - (currentOpts.margin * 2),
					$(window).height() - (currentOpts.margin * 2),
					$(document).scrollLeft() + currentOpts.margin,
					$(document).scrollTop() + currentOpts.margin
                ];
            }
		},

Source

11 thoughts on “Fancybox on iPad/iPhone

    • To fix the IE issue you are having, remove the comment code and contents inside.

      Make this:

      _get_viewport = function() {
      /*return [
      $(window).width() - (currentOpts.margin * 2),
      $(window).height() - (currentOpts.margin * 2),
      $(document).scrollLeft() + currentOpts.margin,
      $(document).scrollTop() + currentOpts.margin
      ];*/
      var uagent = navigator.userAgent.toLowerCase();

      this:

      _get_viewport = function() {
      var uagent = navigator.userAgent.toLowerCase();

  1. In addition to replacing the HTML special entities (> becomes > and & becomes &) You should also add in the margin options in the _get_viewport function:

    return [
    viewportwidth - (currentOpts.margin * 2),
    viewportheight - (currentOpts.margin * 2),
    $(document).scrollLeft() + currentOpts.margin,
    $(document).scrollTop() + currentOpts.margin
    ];

  2. I get a syntax error on the following line:

    if(uagent.search(‘ipad’) > -1 || uagent.search(‘iphone’) > -1 || uagent.search(‘android’) > -1) {

    simple cut and paste, any thoughts?

  3. Thanks allot guys – If you’re having problems with this, read all the comments. If you do what Florent and Judah say, this works great.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">