Hacker News new | past | comments | ask | show | jobs | submit login

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.




+1 for jQuery dimensions. That plugin is a godsend for doing things like this.


scrollTop(), offset(), and the like are built in to the latest jQuery.

I love jQuery.


Excellent! jQuery's what I've been using already. Thanks a ton!




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: