4 ms·
I did this in jQuery at one time. Hopefully this is helpful. $(window).scroll(function() { var offsetTop = $(window).scrollTop(); $("#target").css({
by qhoxie 18y ago
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.
- davidjeffries 18y ago+1 for jQuery dimensions. That plugin is a godsend for doing things like this.
- sam_in_nyc 18y agoscrollTop(), offset(), and the like are built in to the latest jQuery. I love jQuery.
- unalone 18y agoExcellent! jQuery's what I've been using already. Thanks a ton!