News for hackers. Stuff that matters.

Ask HN: How do you create a "limited fixed div"?

I feel a bit embarrassed asking an HTML/JavaScript question, but this is tough because I don't know exactly how you'd describe this effect easily.

What I'm looking to do is create a div that moves along with the user's scrolling - exactly how a fixed div does - but that only does so within the confines of a certain space. I have a site design that's split into two pieces. I want the div to move within the top space, stop when it gets to the bottom space. After that it quits moving down.

How would I get this to work? The problem is, I'm not at all familiar with using JavaScript to manipulate physical location, so I'm not certain how exactly I'd make this solution. And, to the best of my knowledge, this isn't something that HTML/CSS can do on its own: fixed is all-or-nothing.

( 10 comments | 16 points | on HN )
Re:Ask HN: How do you create a "limited fixed div"? by qhoxie on Saturday January 10, 2009 @03:45AM (#427815)
I did this in jQuery at one time. Hopefully this is helpful.

  $(window).scroll(function() {
    var offsetTop = $(window).scrollTop();
    $("#target").css({top: offsetTop});
  });
To explain, whenever the viewport is scrolled, the offset of the viewport to the top of the page is set (offsetTop). Then the #target element is moved to follow the viewport. That will give a fixed effect, so what you need to do is set the max offsetTop to (max depth - #target height).

Note: Dimension type calculations vary widely among browsers, so using a library like jQuery dimensions is quite helpful.

Re:Ask HN: How do you create a "limited fixed div"? by davidjeffries on Saturday January 10, 2009 @04:46AM (#427841)
+1 for jQuery dimensions. That plugin is a godsend for doing things like this.
Re:Ask HN: How do you create a "limited fixed div"? by sam_in_nyc on Saturday January 10, 2009 @04:52AM (#427846)
scrollTop(), offset(), and the like are built in to the latest jQuery.

I love jQuery.

Re:Ask HN: How do you create a "limited fixed div"? by unalone on Saturday January 10, 2009 @08:14PM (#428618)
Excellent! jQuery's what I've been using already. Thanks a ton!
Re:Ask HN: How do you create a "limited fixed div"? by jreposa on Saturday January 10, 2009 @10:38AM (#427982)
Here's a smooth scrolling div within boundaries based on a timeout.

  var attrs = {};
  $(document).ready(function(){
    attrs.startY = $("#target").offset().top;

    // pad the bottom by 20 pixels including the 20 pixels from top added in fixed()
    attrs.endY = $('#bottom').offset().top - $("#target").height() - 40;

    var t = null;
    $(window).scroll(function(){
      clearTimeout(t);
      t = setTimeout('fixed()', 500);
    });
  });

  function fixed() {
    var offsetTop = $(window).scrollTop();
    if (offsetTop < attrs.startY) {
      offsetTop = attrs.startY-20; // get rid of 20 pixel padding when at the top
    } else if (offsetTop > attrs.endY) {
      offsetTop = attrs.endY;
    }
    $("#target").animate({top: offsetTop+20}, 500); // add 20 pixel padding from the top
  }
Re:Ask HN: How do you create a "limited fixed div"? by unalone on Saturday January 10, 2009 @08:15PM (#428621)
Smooth scrolling meaning there's no jump, right? Or meaning it accelerates/decelerates?
Re:Ask HN: How do you create a "limited fixed div"? by calvin on Saturday January 10, 2009 @04:14AM (#427830)
If you use Prototype, you'll want to look at the API docs for document.viewport and getHeight

-- http://www.prototypejs.org/api/document/viewport

-- http://www.prototypejs.org/api/element/getheight

Re:Ask HN: How do you create a "limited fixed div"? by zacharydanger on Saturday January 10, 2009 @12:01PM (#428044)
I'd recommend posting this question over at stackoverflow.com, too.
Re:Ask HN: How do you create a "limited fixed div"? by unalone on Saturday January 10, 2009 @08:15PM (#428619)
I did, and got nothing useful back. Oh well, maybe next time.
Re:Ask HN: How do you create a "limited fixed div"? by HendrikR on Sunday January 11, 2009 @05:51AM (#429061)
There's an article discussing this topic on ajaxian.com featuring another jQuery solution: http://ajaxian.com/archives/pegs-automate-display-fixed