I solved this using jQuery, the hashchange plugin proposed by @barius and the following code:
jQuery(function($){ var $content = $('#contents'); $(window).hashchange(scrollHashIntoView); function scrollHashIntoView(){
The offsetRelativeTo()
code was necessary for my more complicated case when one of the sub-anchors was inside the position:relative
parent (which itself was inside the scrollable content).
I made scrollHashIntoView()
separate function, because (a) it helps to self-document the behavior, and (b) it allows me to call it separately if necessary, and (c) it separates the execution of this work from the brief registration of events and their actions.
A more reliable solution (handling an unlikely but possible case of nested scroll areas) will find the identifier and offsetParent
up to the offsetParent
s tree, offsetParent
each as needed.
source share