1/* 2Internal link topbar offest adjust Javascript 3Code provided by @makshh on GitHub 4 5Bug report on material-mkdocs 6 https://github.com/squidfunk/mkdocs-material/issues/791 7*/ 8 9// Offset top helper 10function offsetY(elem) { 11 if(!elem) elem = this; 12 var y = elem.offsetTop; 13 while (elem = elem.offsetParent) { 14 y += elem.offsetTop; 15 } 16 return y; 17} 18 19// If a link on the same page is clicked, calculate the 20// correct offset and scroll to that part of the page. 21// 22var links = document.getElementsByTagName('a'); 23for(var i = 0; i < links.length; i++) { 24 links[i].onclick = function (event) { 25 if (this.pathname == window.location.pathname && 26 this.protocol == window.location.protocol && 27 this.host == window.location.host) { 28 event.preventDefault(); 29 if(this.hash.substr(1)){ 30 var o = document.getElementById(this.hash.substr(1)); 31 var sT = offsetY(o) - document.getElementsByClassName('md-header')[0].clientHeight; 32 window.location.hash = this.hash; 33 window.scrollTo(0, sT); 34 } 35 } 36 } 37} 38 39// Slugify supplied text 40function slugify(text){ 41 text = text.toLowerCase(); 42 text = text.replace(" ", "-"); 43 return text; 44} 45 46// If there is a hash in the url, slugify it 47// and replace 48if(window.location.hash) { 49 // Fragment exists 50 slug = slugify(window.location.hash); 51 history.replaceState(undefined, undefined, slug) 52 //window.location.hash = slug; 53 document.location.replace(window.location.href); 54} 55