views:

102

answers:

1

I'm after some information on how to make the page's scroll bar scroll the overflow content of a div instead of the page once its scrolled to a certain point.

The end outcome is to scroll the page to the extent that the header isn't visible anymore, but from that point forward scroll the contents of the div - with the main scrollbar, not two nested scrollbars.

If the user scrolls back up, so that the div contents are all the way at the top again, the page should resume scroll and show the header again.

+1  A: 

You can do it using the scrollTop property of your elements. First you'd need to determine where your header ended (the point to scroll your window to):

var header = $('#header');
var headerBottom = header.offset().top + header.height();

Then you could animate your window's scrollbar to this position:

$(window).animate({scrollTop: headerBottom});

Once that was taken care of, you would then get a reference to whichever element you wanted to then scroll by and animate its scrollTop position (same code as snippet above that scrolls the window).

Finally you would need a scroll event handler so you could determine when it was time to change which element you were scrolling the content by:

$(window).scroll(function(){
    // logic to determine which element should be scrolling
});    
Pat
Thanks. I don't think I've explained this very well. The bit I'm really after is what goes inside the `$(window).scroll()` handler to stop the page from scrolling and instead start scroll the div.
Michael Shimmins
Based on the stuff you've got already, I can worked out how to determine when to stop scrolling the page, but its transferring the scroll action from page to div that's stumping me.
Michael Shimmins
Here's a simple example showing the window scroll and then onComplete, the div scroll: http://jsfiddle.net/TnksT/
Pat
Hi Pat. Thanks for taking the time to put that together, but thats not the desired behaviour I'm trying to explain. I don't want it to auto scroll to a given position as it does. What I'm after is scrolling the contents of the red div using the main window's scrollbar once the blue div has been scrolled off screen. Nothing automatic about it, purely in response to the user's scrolling. I'll try to clarify in the question text above as well.
Michael Shimmins
Ah, my mistake. Hmmm...I'm not sure how to do that either. The best I can do is transfer the window scroll to the div, but that doesn't prevent the window scroll from also moving the entire window's content: http://jsfiddle.net/Q7pRD/
Pat
Thanks for your help. I looked at it from a different perspective last night, and realised I was describing the functionality, but was completely missing how it was done. I've seen this in another system, and just assumed they must have been scrolling a div with the window's scroll bar. Turns out its a bit simpler than that, and involves fixing another div (a toolbar) once the scroll has reached a certain point. Even though in the example I've put together this is pretty obvious, in the reference app I was looking at its done much more subtly.
Michael Shimmins
The reference app's done in such a way that you don't really notice that the toolbar is just covering up the top of the underlying div, through the use of some nice alpha channel fading, and fancy pants borders. I've forked your sample to show (almost) the required functionality. I'd still be after any tips you can give to get it a bit smoother - if you scroll quickly or with the wheel it doesn't align with the top of the document. The reference app doesn't suffer this side affect. http://jsfiddle.net/XwaT3/
Michael Shimmins
Yep, I know that effect well (used it in a few of my apps). Here's an update of your code with the glitches smoothed out: http://jsfiddle.net/XwaT3/1/. One thing that helps speed this up is that there's very little happening in the scroll event handler. The only slowish call is creating a jQuery object from `this`. The rest is initialized before the event handler is run. If it's still glitchy on your system, you could even move this call outside of the event handler.
Pat
Brilliant - thats great. Thanks for your help.
Michael Shimmins