From cadc80893242c2490e9f1c26f8585a5040984f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sat, 2 Apr 2022 15:30:43 +0200 Subject: [PATCH] theme: scroll to heading on initial load #232 --- static/js/theme.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/static/js/theme.js b/static/js/theme.js index 8d7c3f32f4..28152ea9db 100644 --- a/static/js/theme.js +++ b/static/js/theme.js @@ -484,7 +484,7 @@ function initSwipeHandler(){ function scrollToActiveMenu() { window.setTimeout(function(){ - var e = $("#sidebar ul.topics li.active a")[0]; + var e = document.querySelector( '#sidebar ul.topics li.active a' ); if( e && e.scrollIntoView ){ e.scrollIntoView({ block: 'center', @@ -493,6 +493,20 @@ function scrollToActiveMenu() { }, 10); } +function scrollToFragment() { + if( !window.location.hash || window.location.hash.length <= 1 ){ + return; + } + window.setTimeout(function(){ + var e = document.querySelector( window.location.hash ); + if( e && e.scrollIntoView ){ + e.scrollIntoView({ + block: 'start', + }); + } + }, 10); +} + // Get Parameters from some url var getUrlParameter = function getUrlParameter(sPageURL) { var url = sPageURL.split('?'); @@ -544,6 +558,7 @@ jQuery(function() { initSwagger(); initMenuScrollbar(); scrollToActiveMenu(); + scrollToFragment(); initLightbox(); initImageStyles(); initToc();