I want to postion a DIV
inside a page such that it is visible to the user even if the user vertically scrolls the page.
The page has a heading at the top of the page which is 75 px
tall. Now when the user is at the top of the page and has not scrolled vertically, the DIV
must be postioned below the heading. However, once the user scrolls the page causing the heading to go out of sight, the same DIV
must now be position at the top of the page (i.e. near the top edge of the browser viewport)
My big concern is the support for window.onscroll
event on browsers. I checked QuirksMode for compatibility (http://www.quirksmode.org/dom/events/scroll.html). It seems to have decent compatibility on IE and Firefox. However the Safari and Chrome support seems a bit quirky. And both these browsers are part of my target browsers' list.
Can anybody tell me if the window.onscroll
event is an effective way of detecting page/frame scrolls? Any other suggestions?
P.S. I have considered using the CSS position: fixed
rule. It is close to the solution but the DIV
is just stuck to one position and I cannot have it adaptively move based on the visiblity of the heading.
Thanks!
If you read about the clunkiness in WebKit on Quirksmode, you'll notice the following:
Safari (but not on iPhone) and Chrome seem to monitor scrollTop acces in order to determine whether the user has scrolled an element. The log function of my test script changes scrollTop regularly, and Safari responds by firing a scroll event. Since the logging of this event changes the log element's scrollTop once more, scroll events will be fired continuously. All this happens when the log element at the bottom of the page doesn't have a scrollbar yet, when it has a normal overflow: visible, and even when I set scrollTop to 0 each time a new event is logged. The buggy behaviour stops only when I remove the scrollTop line entirely.
This issue shouldn't affect what you're trying to achieve since you're not setting the scrollTop
of any element. You're attaching onscroll
to the window, which appears to have no issues between any browser anyway.