소스 검색

Always scroll to content (if availabl). Use smooth scroll for location.hash

Riccardo Balbo 4 년 전
부모
커밋
60eccdc6b9
1개의 변경된 파일12개의 추가작업 그리고 2개의 파일을 삭제
  1. 12 2
      static/js/utils.js

+ 12 - 2
static/js/utils.js

@@ -82,8 +82,7 @@ function lazyLoad(parent) {
 
 
 window.addEventListener("DOMContentLoaded", function () {
-    const contentAnchor=document.querySelector("#content");
-    if(contentAnchor&&!isInViewport(contentAnchor)){
+    const scrollTo=(contentAnchor)=>{
         contentAnchor.scrollIntoView({
             behavior:"smooth",
             block:"start",
@@ -91,6 +90,17 @@ window.addEventListener("DOMContentLoaded", function () {
         });
         console.log("Scroll content into view");
     }
+    if (!location.hash) {
+        const contentAnchor=document.querySelector("#content");
+        if(contentAnchor)scrollTo(contentAnchor);
+    }else{
+        setTimeout(()=> {
+            window.scrollTo(0, 0);
+            const contentAnchor=document.querySelector(location.hash);
+            scrollTo(contentAnchor);   
+        }, 1);
+        
+    }
 
     document.querySelectorAll("[toggle]").forEach(el => {
         const toggleId = el.getAttribute("toggle");