0

I have the following script included in my fancybox script:

beforeShow: function () {
    var id = this.element.attr("id")
    if (id) {
        window.location.hash = id;
    }
},                  
beforeClose: function() {
    window.location.hash = "";
}

Basically if I open an image with fancybox, the url will change to /#image and when the fancybox window is closed, the url changes to /#. But when this happens, the page scrolls back to the top. Is there any way to avoid this?

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
coldpumpkin
  • 711
  • 4
  • 14
  • 30
  • 1
    Check out this question: http://stackoverflow.com/questions/645202/can-i-update-window-location-hash-without-having-the-web-page-scroll – LeonardChallis Sep 19 '12 at 13:17

1 Answers1

1

You may store previous value:

var scroll = $(window).scrollTop();
// change hash to #
$(window).scrollTop(scroll);
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
  • Thank you. However, is it possible for it not to scroll to the beginning of the image and instead load the previous scroll location? – coldpumpkin Sep 19 '12 at 16:50