Twitter uses the css: position: fixed; property position: fixed; which is undoubtedly the best way.
This does exactly what he says, he fixes the position. Using the top , right , bottom and left properties, you can set the exact position of your div.
Edit 13-12-11 (amazing date!)
Property position: fixed; cannot affect the positioning property on only one axis. This means that you cannot scroll left or right as you want.
I highly recommend that you either avoid exceeding the screen width by using percentages for your element width. You can also just stick with your javascript.
However, you could first use the method that I proposed, but change the left property using the scroll event listener, so that when scrolling, the left offset will increase or decrease. Since jQuery support for cross browsers, I would go for jQuery. I think you can do pretty much the same with the prototype library, but I am not familiar with this library.
jQuery (works in google chrome):
var offset = 400; // left offset of the fixed div (without scrolling) $(document).scroll(function(e) { // b is the fixed div $('.b').css({ 'left': offset - $(document).scrollLeft() }); });
Watch a live demo
You may need to change the document object to another object or selector of your choice.
source share