views:

133

answers:

2

The question is confusing, I know, but I just couldn't put it any other way.

Here's the url: Configure - Apple Store (U.S.)

The 'Summary' box on the side is aligned below the sub-header (with pictures of all the Mac models) and is at the same height as the content block. If you scroll the page up, the whole page goes up as expected. However, the moment the content block scrolls off the viewable area of the browser window, the summary block anchor itself to the top level and stays there even if you scroll to the far end of the page.

What is this behaviour called and what's the simplest, cleanest way to achieve it? I'd prefer a jQuery plugin and/or code snippet instead of plain JavaScript.

+2  A: 

You can do something like this:

$(function () { 
  var $el = $('.fixedElement'), 
      originalTop = $el.offset().top;  // store original top position

  $(window).scroll(function(e){ 
    if ($(this).scrollTop() > originalTop ){ 
      $el.css({'position': 'fixed', 'top': '0px'}); 
    } else { 
      $el.css({'position': 'absolute', 'top': originalTop}); 
    } 
  }); 
});

Check an example here.

CMS
Ace! Thanks for this!
aalaap
In your example, the <script> tag is at the end of the content. Is that compliant or would it be more advisable to put it in the <head> under a $(document).ready() event?
aalaap
Well, is a recommended practice to put script tags as lowest possible in the document, (http://is.gd/38y6u), but you will have no problems if you put it on the head...
CMS
That's a very interesting tip - thanks!
aalaap
A: 

I think you are looking for

jquery-scroll-follow

plugin

rahul
The code snippet above is extremely trim, so a plugin is unnecessary. Thanks anyway!
aalaap