Fancybox shifts absolute/fixed positioned elements
Page elements that are positioned to the right either with position: absolute; or position: fixed; will shift to the right when a Fancybox opens. Possible fix: add the right-margin that is applied to the body to such elements as well.
Example: http://jsfiddle.net/hkDnQ/
I do not think that it will be implemented. As alternative, you can do it yourself using callbacks or turn that feature off - http://jsfiddle.net/RLrAb/ (the drawback is that scrolling would scroll the whole page, too).
Hi, first just want to say I absolutely love the new Fancybox - well done! Have found a couple of issues though. I believe this bug here raised by collideous is one of them.
It seems to be due to this new feature as you mention, and turning it off does fix it. But unfortunately means the whole page scrolls as you say, which is a shame as that feature is a major strength of this new release.
Anyway, to add to the information on here, I've found that the shift occurs for me only when there is a vertical scrollbar. In a browser window with no scrollbar (where the content is all visible), it doesn't occur. So it seems to me that basically the code is not acounting for the width of the vertical scrollbar.
Do you think this is something that might be fixed at some point (even in a later release)? Or will I have to live with turning this great new feature off?
Thanks again and keep up the good work!
Well, the explanation is quite simple. This happens if -
- the page has vertical scrollbars
- elements are positioned to the right side of page
then the script removes scrollbars from the page and the page actually gets wider, therefore these elements shift to the right. The margin is added to the body element to compensate that width change, but that does not apply to fixed/absolute positioned elements (without this margin change, the whole contents would shift to the right).
Ah ok, thanks. You mentioned using callbacks as a workaround? I don't know anything about callbacks but I will look into it! Cheers!
Although I am I right in thinking that the vertical scrollbar is not visually removed from the page - just disabled? That's the behaviour I am seeing in my browsers.
The behaviour I am seeing is that my centred container element is shifted to the right. I am guessing that's because of the "auto" left and right margins? Anyway, its CSS is as follows:
#container { display: block; position: relative; overflow: hidden; width: 1000px; height: 800px; margin: 15px auto 15px auto; z-index: 1; }
I have fixed the issue of the centred container div moving on my site by adding 2 lines into your FancyBox JS code. Where you add to and reset the body width, I have added lines that also do this to the html width.
ie.
$('html').css('margin-right', getScalar( this.margin ) + obj.scrollbarWidth);
and
$('html').css('margin-right', that.margin);
I hope this is helpful to you. Please let me know if you think this might cause any other issues or if I have missed anything.
All the best!
I fixed my layout shifts by determining how far a right-aligned element is from the left, clearing the right style property and give the element the calculated left distance.
$.extend($.fancybox.defaults, {
beforeLoad: function() {
if (!fancyFix) {
fancyFix = true;
$("#element").css("right","").css("left",function() {
return $(window).width() - $(this).width();
});
}
}
});
Obviously, if someone resizes the browser window, the element will now remain at that fixed distance from the left. But how often does one resize the browser while viewing a page?
I have the same problem. Centered div with margin: auto; and a few absolute positioned elements. I have now turned off this new feature with fixed: false and now these shifts do not happen anymore when I click the image. However, they DO happen if I close the image. So I send the image back into the background and my whole content shifts to the right edge of my browser window.
I hope there is a fix for this bug, as this makes fancybox completely unusable for me.
@espressoist @collideous
I actually do not see an issue there. If you really care that something is shifting, then either make overlay background darker so it is less noticeable or use callbacks to adjust position (or simply show and hide them).
The next minor release will contain an option to turn this feature off, but your visitors will miss this user experience enhancements.
@petze could you show your page? I do not see any shifting - http://jsfiddle.net/pWehy/
I have my body tag formatted as follows:
body { width: 960px; margin: 0 auto; }
This leads to the problem. If however, I pack the whole content on my page into an additional div, and use the same styling on this div instead on the body-tag there is no more shift. If I turn the 'fixed' property back on, I only notice that the overlay will not extend over that area where the scrollbar usually is. So the scrollbar is hidden, but instead this whole area is not white. However, this hadly cannot be called a 'bug'. So the only thing I needed to do to fix my problem what to remove the styling from the body tag and use an additional container instead.
I have the same problem like petze but with an additional absolute div in the background.
Demo: http://jsfiddle.net/mqYqu/ (resize the demo-window width to min 1000px to see the effect)
With fixed:true the background is shifting to the right side, with false not. But this only occours if the content is long enough and a vertical scrollbar is already present. (all browsers)
If the content is short without a scrollbar, the background shifts when fixed:false - the opposite behavior. But this second demo works only on IE.
Demo: http://jsfiddle.net/bqaLX/ (resize again to min 1000px)
So we need a fix here to remove both behaviors.
Well, it is not possible to fix every possible scenario where something is shifting, for example, if the page has centered background image.
But you can disable this feature - http://jsfiddle.net/RLrAb/
I just came across a very similar problem:
When opening FancyBox, my whole page would scroll to the top and shift things to the left. My quick solution, with just messing with the css, was the following in my default stylesheet:
.fancybox-lock{ overflow:visible !important; // Solves moving the page to the top margin-right:0px !important; // Solves the shifting of my whole page to the left }
Hope this helps anyone out there. Cheers!
Woah, thank you so much @davesierra, was about jumping from the balcony because of that...!!!
The CSS you provided finally solved this odd problem...but why doesn't anybode else complain about this^^...?
Great @davesierra!!!
In my case I needed margin-right to be auto, but FancyBox was introducing an element.style margin-left:15px...
I also had to change it a little bit:
body.fancybox-lock{
overflow:visible !important;
margin-right:auto !important;
}
I had a similar problem. Here's what fixed it
Previous code:
body {
width: 960px;
margin: 0 auto;
}
Fixed code:
body {
width: 960px;
margin: 0 auto !important;
}
If you're using isotope filtering or similar solution you might also want to add this:
html {
overflow-y: scroll !important;
}
Perfecto, thanks @davesierra!!!
All of these solutions seem to still allow users to scroll in the background behind the popup. If you'd like to prevent that and still not show the extra margin, one solution is to do this inside the ready function (note: this is for FancyBox2, but you might be able to retrofit it to work with the original, too):
$body = $('body');
$window = $('window');
$('.fancybox').fancybox({
beforeShow : function() {
$body.bind('mousewheel DOMMouseScroll', function(e){
this.scrollTop = $window.scrollTop();
e.preventDefault();
});
},
afterClose : function(){
$body.unbind('mousewheel DOMMouseScroll');
},
helpers : {
overlay : {
locked : false
}
}
});
Before showing the popup, scrolling on the body is locked and set to the current position scrolled thus far (in the beforeShow method). After the user closes the popup, you unbind this behavior (in the afterClose method). Lastly, you disable the overlay helper that locks scrolling (which is generating the pesky margin in the first place).
Works Fine..... Thankx petze