2

I have a dynamic page where clicking a link triggers some javascript that sets some page elements to display:none, and thus changes the height of the page. The typical browser behavior for this is that the scroll position from the top of the page is retained. I want to retain the scroll position from the bottom instead, because the link is near the bottom of the page and I would like the user's cursor to remain over the link after it is clicked.

The code I have to do this is:

var scrollBottom=getDocHeight()-getScrollTop();
//do stuff to change height
window.scrollTo(0,getDocHeight()-scrollBottom);

(using cross-browser functions I found to obtain document height and scroll position).

This works fine in chrome and internet explorer, but in firefox, there is a small delay between the page height changing and the scroll position changing. As a result there is a flicker as the page quickly realigns itself, which is bothering me a great deal.

Can anyone suggest a fix or a more natural way to remember the scroll position from the bottom of the page instead of the top?

Thank you.

Matt
  • 21
  • 1
  • 2
  • Take a look here, might be the same issue: * http://stackoverflow.com/questions/1904118/firefox-jquery-scrollto-flicker-bug – icyrock.com Nov 25 '10 at 02:51
  • I did see that. I don't think it's the same problem though because I'm definitely cancelling the default browser action (I have `return false` in the `onclick` event.) It looks like it may be a similar issue to Darryl's follow-up question but that went unanswered. – Matt Nov 25 '10 at 03:32
  • Did you try to use `e.preventDefault()` also? – icyrock.com Nov 25 '10 at 03:34
  • Could you point me to something explaining that? I'm not using jQuery, which was all I could turn up in a search. – Matt Nov 25 '10 at 03:37
  • This SOq: http://stackoverflow.com/questions/1357118/javascript-event-preventdefault-vs-return-false, this reference: https://developer.mozilla.org/en/DOM/event.preventDefault – icyrock.com Nov 25 '10 at 03:40
  • It doesn't seem to help, unfortunately. – Matt Nov 25 '10 at 03:45
  • Hm... The only other thing I can suggest is removing `return false;` and just relying on `preventDefault`. If you would like to try it, I suggest using jQuery's fadeOut: http://api.jquery.com/fadeOut/. It's not hard to set up and will probably make your life a lot easier for other things, too. – icyrock.com Nov 25 '10 at 03:57
  • OK, thanks for the help. If I can't find an ideal solution I'll look at workarounds like that. – Matt Nov 25 '10 at 04:00
  • Np, hope you are going to find something that works! – icyrock.com Nov 25 '10 at 04:06

0 Answers0