107

I have implemented fancybox2 on a dev site.

When I engage the fancybox (click the link etc) the whole html shifts behind it - and goes to the top. I have it working fine in another demo, but when I drag the same code to this project it jumps to the top. Not only with the links to inline divs, but also for simple image gallery.

Has anyone experienced this?

sheriffderek
  • 8,217
  • 6
  • 38
  • 64
  • note: I just checked this on the ipad an the iphone and this problem is not occurring... however it is in chrome and firefox. – sheriffderek Nov 25 '12 at 00:36
  • 1
    If you are using fancybox v2.1.5 it seems that the issue has been fixed in the latest master that you can download form here https://github.com/fancyapps/fancyBox/archive/master.zip so no more hacks to either the js or css files. – JFK Sep 21 '13 at 19:36

10 Answers10

336

This can actually be done with a helper in Fancybox 2.

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

http://davekiss.com/prevent-fancybox-from-jumping-to-the-top-of-the-page/

jsnjack
  • 2,282
  • 2
  • 17
  • 20
Dave Kiss
  • 9,778
  • 10
  • 49
  • 74
  • 11
    This is definitely the way to do it. OP sould mark this as accepted answer. Thank you for this! – cfx Sep 24 '13 at 22:15
  • Totally agree. The accepted answer (to remove code from fancybox2) is a hack. – Rob Gonzalez Oct 02 '13 at 15:12
  • Its unfortunate the accepted answer hasn't been changed in a full year. Hope future users implement this and not the "chop code out of the plugin" answer. – AndyWarren Oct 17 '13 at 14:13
  • 5
    I think this answer doesn't solve the issue. Many people **may not like** to be able to scroll the content behind fancybox (this what will happen if `locked` is set to `false`.) This is a temporary workaround to an issue that may create another for some scenarios. This was a bug and it has been corrected in the latest master https://github.com/fancyapps/fancyBox/archive/master.zip – JFK Nov 13 '13 at 00:47
  • 1
    @JFK I agree that this fix may not actually be ideal because of the scrolling, but I've tested it with the latest master and the page still jumps to the top. It does revert back to the original position when fancybox is closed though. – Mr Jonny Wood Jan 22 '14 at 16:45
  • @MrJonnyWood : if you have a demo, I would love to see it ;) – JFK Jan 22 '14 at 17:25
  • @JFK I'm working locally at the moment, but if I get chance I'll post something to a hosted dev site. – Mr Jonny Wood Jan 23 '14 at 11:02
  • @JFK - This is happening to me in v2.1.5. I opened a [GitHub Issue](https://github.com/fancyapps/fancyBox/issues/860). – L84 May 03 '14 at 22:47
  • @Lynda : the fancybox js file you use in your demo, doesn't match the one from the last master https://github.com/fancyapps/fancyBox/archive/master.zip – JFK May 04 '14 at 03:36
  • @Lynda : here your page using fancybox from latest master http://www.picssel.com/playground/jquery/lynda/ that doesn't jump to the top of the page. – JFK May 04 '14 at 03:47
  • This doesn't seem to be working in the latest release of fancybox. – deadboy May 20 '15 at 14:08
  • great stuff. saves me minutes trying to fix luckily I've read the post! – lidongghon Aug 25 '15 at 14:06
  • @EvinUgur: For me this solution works great with version 2.1.5 (which is the latest version). – Fabian Schöner Sep 22 '15 at 10:15
  • I've added my own answer which actually fixes the issue rather than avoiding it. This answer isn't a solution the the actual problem. – Ian May 13 '16 at 16:48
  • Using latest version, (2.1.5). if i have this set as described and the contents of the fancybox are longer than the screen, then I can't scroll down in IE. it keeps jumping back tot he top of the fancybox. but only in IE... – Adam Nov 16 '16 at 23:26
35

Jordanj77 is correct but easiest solution is to just go to stylesheet jquery.fancybox.css and comment out the row saying overflow: hidden !important; in section .fancybox-lock

fast-reflexes
  • 2,146
  • 2
  • 23
  • 32
  • 4
    This is better than using helper because with helper the page continues to scroll underneath the overlay with the mouse wheel. Commenting out the overflow keeps it from jumping but also scrollbar is still locked for the main page. – Panama Jack Aug 27 '13 at 05:53
14

I realize this is an old question, but I think I have found a good solution for it. The problem is that fancy box changes the overflow value of the body in order to hide the browser scrollbars.

As Dave Kiss points out, we can stop fancy box from doing this by adding the following parameters:

$('.image').fancybox({
  padding: 0,
  helpers: {
    overlay: {
      locked: false
    }
  }
});

But, now we can scroll the main page while looking at our fancy box window. It is better than jumping to the top of the page, but it is probably not what we really want.

We can prevent scrolling the right way by adding the next parameters:

    $('.image').fancybox({
      padding: 0,
      helpers: {
        overlay: {
          locked: false
        }
      },
    'beforeLoad': function(){
      disable_scroll();
        },
     'afterClose': function(){
       enable_scroll();
      }
    });

And add these functions from galambalaz. See: How to disable scrolling temporarily?

    var keys = [37, 38, 39, 40];

    function preventDefault(e) {
      e = e || window.event;
      if (e.preventDefault) e.preventDefault();
      e.returnValue = false;  
    }

    function keydown(e) {
        for (var i = keys.length; i--;) {
            if (e.keyCode === keys[i]) {
                preventDefault(e);
                return;
            }
        }
    }

    function wheel(e) {
      preventDefault(e);
    }

    function disable_scroll() {
      if (window.addEventListener) {
          window.addEventListener('DOMMouseScroll', wheel, false);
      }
      window.onmousewheel = document.onmousewheel = wheel;
      document.onkeydown = keydown;
    }

    function enable_scroll() {
        if (window.removeEventListener) {
            window.removeEventListener('DOMMouseScroll', wheel, false);
        }
        window.onmousewheel = document.onmousewheel = document.onkeydown = null;  
    }
Community
  • 1
  • 1
Joeran
  • 878
  • 6
  • 12
9

The problem is that fancyBox changes the overflow value of the body in order to hide the browser scrollbars. I couldn't find an option to toggle this behavior.

You could remove this section of the fancyBox code to prevent it:

if (obj.locked) {
    this.el.addClass('fancybox-lock');

    if (this.margin !== false) {
        $('body').css('margin-right', getScalar( this.margin ) + obj.scrollbarWidth);
    }
}
jordanj77
  • 223
  • 1
  • 9
  • YAY! That was also why my page was jumping 20px to the left then. Thanks! You solved 2 of my problems! – sheriffderek Nov 25 '12 at 06:37
  • The scroll bars are there... but I always use overflow-y: scroll; anyways, so it's a fair trade-off for me. – sheriffderek Nov 25 '12 at 06:38
  • I guess that would block the mouse-wheel functionality - I will continue to look for other solutions as well. But like I said, this is doing the trick for now! – sheriffderek Nov 25 '12 at 06:46
  • 3
    If you are using fancybox v2.1.5 it seems that the issue has been fixed in the latest master that you can download form here https://github.com/fancyapps/fancyBox/archive/master.zip so no more hacks to either the js or css files. – JFK Sep 21 '13 at 19:37
  • You can also add this to your CSS: .fancybox-lock { overflow: inherit !important; } – sebastien Apr 05 '14 at 19:42
  • 1
    This was the best answer at the time... and if for some reason you have a legacy site or something --- see this as some reference. – sheriffderek Apr 10 '14 at 05:24
6

Despite the fact, that the proper way of solving this problem is via the options, which fancybox provides (refer to this answer), CSS could be used to solve the problem. There is no need to edit the library's css file, just add this to the main stylesheet of the application:

html.fancybox-lock {
    overflow: visible !important;
}

The code resets the original overflow of the element. The problem is that overflow: hidden; hides the scrollbar on the <html> element, which causes the page to "jump" to the top. In order to preserve the position of the scrollbar, we overwrite the overflow with overflow: visible;

Community
  • 1
  • 1
thexpand
  • 601
  • 14
  • 26
4

This also helped

.fancybox-lock body {
    overflow: visible !important;
}
hsobhy
  • 1,403
  • 2
  • 18
  • 34
  • 1
    Overwriting 3rdparty styles isnt a good idea especially if you have to use a !important modifier to break the cascade. I encourage everyone to use the fix provided by Dave Kiss above, not this one. – Fabian Schöner Sep 22 '15 at 10:15
1

The accepted answer is not complete as it does not actually solve the problem it just avoids it! Here is a more complete answer that actually gives you the desired functionality while fixing the window jump issue:

        $('.fancybox').fancybox({
            helpers: {
                overlay: {
                    locked: false
                }
            },
            beforeShow:function(){ 
                $('html').css('overflowX', 'visible'); 
                $('body').css('overflowY', 'hidden'); 
            },
            afterClose:function(){ 
                $('html').css('overflowX', 'hidden');
                $('body').css('overflowY', 'visible'); 
            }
        });
Ian
  • 643
  • 3
  • 12
0

Maybe this answer is alil late but maybe it could help in the future, if after clicling/closing a image fancybox makes your website scroll to the top, you could just delete the :

F.trigger('onReady');

or better use:

/*F.trigger('onReady');*/

In fancyBox version: 2.1.5 (Fri, 14 Jun 2013) that part of the code is at line 897.

Nahomy Atias
  • 193
  • 1
  • 2
  • 6
0

You can actually code like this if you are using fancybox default function:

    $(document).ready(function() {
        $(".fancybox").fancybox({
            padding: 0,
            helpers: {
                overlay: {
                  locked: false
                }
            }
        });
    });
Steele Rocky
  • 551
  • 5
  • 7
-4

I fixed it with:

overflow: hidden !important; 

in .fancybox-lock body in jquery.fancybox.css. And scrollbar not jumping :)

Artless
  • 4,362
  • 1
  • 23
  • 39