I've been finding uses for movable objects on the screen for a long time now, ranging from tinkering with in-browser wireframing to a recent
interactive resume concept. While working on the latter, I realized that, depending on the browser (read: when using Internet Explorer), I was having trouble maintaining a reliable position of the element at the location of the mouse pointer as the screen scrolls down. I finally got frustrated enough to revisit my moving script altogether and refine it drastically. I've produced
this sandbox to demonstrate the concept, and here are a few things to note:
(1) Note that the boxes are both visually, and in code, embedded in each other. In other words, C is in B, and B is in A. Note that moving a box with others inside of it initially moves the concepts as well. However once you move a box outside of its container (though this is a setting that I plan to make optional by adding a paramter to the 'grab' function), it is detached as a child of the container and reattached to the grandparent (as long as the parent isn't the main document element).
(2) There is a distinction between the .offsetTop/.offsetLeft values, which represent the position value within a relative or absolute container, and the getX()/getY() values that represent absolute X/Y (top/left) values from the top of the screen. By keeping track of the position of the mouse when grabbed, the absolute X/Y of the parent element, the offset X/Y of the dragged element, and the current position of the mouse as it is dragged, I can continually update the relative position of the element in order to keep it reliably attached to the mouse (and even taking into consideration the grab point within the element so that it doesnt' jump) in all (or most?) browsers (including the IE8 that I tested it in after seeking the solution).
(3) When an element is dragged out of its parent container, the checkParent() function (which is constantly called during dragging anyway) also accounts for the difference between the absolute offsets of the parent and the new (grand)parent element, eliminating the jump that would have otherwise occurred there as an element were detached.
The code is suprisingly simple once you grab the various values and sort out the math. You can
take a look at it here, and you're welcome to use it, but please take the time to credit the code to this site if you do!