Created
June 5, 2020 21:25
-
-
Save simevidas/ff6a69cf46642ff0047f7093b79d354d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if the URL contains a hash, scroll to the associated heading | |
if (location.hash) { | |
let media = body.querySelectorAll('img, video'); | |
let elem = body.querySelector(location.hash); | |
let mediaPromise = []; | |
for (let m of media) { | |
// “<img>/<video> rendered” promises are needed for a scrollIntoView() re-run | |
mediaPromise.push( | |
new Promise((res) => { | |
if (m.tagName === 'IMG') { | |
if (m.complete && m.naturalWidth) res(); | |
m.onload = () => res(); | |
m.onerror = () => res(); | |
} else if (m.tagName === 'VIDEO') { | |
if (m.readyState >= 3) res(); | |
m.onloadeddata = () => res(); | |
m.onerror = () => res(); | |
} | |
}) | |
); | |
} | |
setTimeout(() => { | |
elem.scrollIntoView({ top: 'start' }); | |
Promise.all(mediaPromise).then(() => | |
elem.scrollIntoView({ top: 'start' }) | |
); | |
}, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment