simple-keyboard/docs/script/inner-link.js
Francisco Hodge 2e3ca5716b Docs update
2018-10-24 18:18:24 -04:00

33 lines
997 B
JavaScript

// inner link(#foo) can not correctly scroll, because page has fixed header,
// so, I manually scroll.
(function(){
var matched = location.hash.match(/errorLines=([\d,]+)/);
if (matched) return;
function adjust() {
window.scrollBy(0, -55);
var el = document.querySelector('.inner-link-active');
if (el) el.classList.remove('inner-link-active');
// ``[ ] . ' " @`` are not valid in DOM id. so must escape these.
var id = location.hash.replace(/([\[\].'"@$])/g, '\\$1');
var el = document.querySelector(id);
if (el) el.classList.add('inner-link-active');
}
window.addEventListener('hashchange', adjust);
if (location.hash) {
setTimeout(adjust, 0);
}
})();
(function(){
var els = document.querySelectorAll('[href^="#"]');
var href = location.href.replace(/#.*$/, ''); // remove existed hash
for (var i = 0; i < els.length; i++) {
var el = els[i];
el.href = href + el.getAttribute('href'); // because el.href is absolute path
}
})();