Fixed jumping to anchor inside closed details

This commit is contained in:
squidfunk
2017-11-05 15:17:23 +01:00
committed by Martin Donath
parent 1c11a78f44
commit c2bbb060dc
4 changed files with 28 additions and 2 deletions

View File

@@ -145,6 +145,32 @@ function initialize(config) { // eslint-disable-line func-style
})
}
/* Open details after anchor jump */
const details = () => {
if (document.location.hash) {
const el = document.querySelector(document.location.hash)
if (!el)
return
/* Walk up as long as we're not in a details tag */
let parent = el.parentNode
while (parent && !(parent instanceof HTMLDetailsElement))
parent = parent.parentNode
/* If there's a details tag, open it */
if (parent && !parent.open) {
parent.open = true
/* Force reload, so the viewport repositions */
const loc = location.hash
location.hash = " "
location.hash = loc
}
}
}
window.addEventListener("hashchange", details)
details()
/* Force 1px scroll offset to trigger overflow scrolling */
if (Modernizr.ios) {
const scrollable = document.querySelectorAll("[data-md-scrollfix]")