User:Renaud Houdinet/vector.js

( function {   // find the toc     var toc = document.getElementById("toc");    var content = document.getElementById("content");

// extract toc content and encapsulate into a div var tocUl = toc.querySelector("ul"); tocUl.parentNode.removeChild( tocUl); var divToc = document.createElement('div'); divToc.appendChild( tocUl ); divToc.classList.add("title-toc"); divToc.style.display = "none";

// remove the old toc toc.parentNode.removeChild( toc );

var title = document.getElementById( "firstHeading" ); // toggle toc when title is clicked title.onclick = function { if( !!( divToc.offsetWidth || divToc.offsetHeight || divToc.getClientRects.length ) ) { divToc.style.display = "none"; } else { var titleRect = title.getBoundingClientRect; divToc.style.display = "block"; divToc.style.position = "fixed"; var tocRect = divToc.getBoundingClientRect; divToc.style.left = (titleRect.left + ( (titleRect.width - tocRect.width) / 2 )) + "px"; divToc.style.top = (titleRect.top + titleRect.height ) + 'px'; }   }

// hide toc on scroll window.onscroll = function { divToc.style.display = "none" var titleRect = title.getBoundingClientRect; if(titleRect.top < 0) { title.style.position = "fixed"; title.style.top = "0px"; } else { var contentRect = content.getBoundingClientRect; if ( contentRect.top >= 0 ) { title.style.position = "relative"; }       }    }

document.body.appendChild( divToc ) } /*	function { var toc = document.getElementById("toc"); toc.classList.add("tochidden"); toc.parentNode.removeChild( toc ); document.body.appendChild( toc ); }*/

);