Tether is a javascript library for efficiently making an absolutely positioned element stay next to another element on the page.
Tether includes the ability to constrain the element within the viewport, it's scroll parent, any other element on the page, or a fixed bounding box. When it exceedes those constraints it can be pinned to the edge, flip to the other side of it's target, or hide itself.