Looking at your site, as there is no position reference using the address bar more specifically the hash
area of window.location
what happens is that when you enter your fancybox
it ends up deleting the scrollbar, taking the value to 0.
A solution is when a person clicks, before displaying their fancybox
, you store in a context variable the position of scroll
and when you close, you apply the scrolling pro navigation you saved, leading to person back to the original position.
First we get the buttons that have fancybox
and give a function to store the value of scrollTop
when clicked:
window.lastScrollTop = $(window).scrollTop(); // Vai iniciar em zero;
// Quando clicar no fancybox vamos salvar um novo valor
$('a.fancybox').on('click', function () {
window.lastScrollTop = $(window).scrollTop();
});
Now when you exit fancybox
has to return to the stored value, then in your settings, you must add onClosed
handler .
$(document).ready(function ($) {
$('.fancybox').fancybox({
// ...
onClosed: function () {
// Ao fechar leva de volta a posição armazenada anterior do scrollTop
$(window).scrollTop(window.lastScrollTop);
},
// ...
});
});
I used a global variable lastScrollTop
because I do not know how your code was written and I do not know how to isolate the context for this case. But I do not recommend using global variables.
Another more generic way is also possible, instead of adding the click event to the anchors, is to use the fancybox% handler , it is theoretically called before applying its effect that will eliminate the onStart
, would look like this:
$(document).ready(function ($) {
var lastScrollTop;
$('.fancybox').fancybox({
// ...
onStart: function () {
// Armazenando o scrollTop antes de iniciar o processo scrollTop
lastScrollTop = $(window).scrollTop();
},
onClosed: function () {
// Ao fechar leva de volta a posição armazenada anterior do scrollTop
$(window).scrollTop(lastScrollTop);
},
// ...
});
});
In this way it is possible to use a context variable not exposing any global variables.