To do it correctly, you need to track the up to 4 different element positions.
You've got your actual sticky element, then you've got the parent of the element to ensure it's within the view port. Then you've got the scroll container as well. Plus, you might have a constrain/target node, to ensure that the sticky element doesn't extend passed an artificial bounding box.
Overall, doing all of these domGeometry/position calculations on each window.onscroll event leads to extremely inefficient JS. I ended up removing the Sticky altogether because on iOS it would basically make my application unusable.
Here was my implementation[1]. It was considerably harder to created than I expected it would be... coding advice/tips welcome.
[1] : https://gist.github.com/Kalyse/6534936 (My personal Sticky implementation using Dojo).