diff options
author | amazingrise <8315221+AmazingRise@users.noreply.github.com> | 2021-05-14 12:43:01 +0300 |
---|---|---|
committer | amazingrise <8315221+AmazingRise@users.noreply.github.com> | 2021-05-14 12:43:01 +0300 |
commit | 785ad8425cbc1390e28180486c550f5e3a4232f6 (patch) | |
tree | 96360d3136a083008ab3d1129e04e1aa96af2449 | |
parent | a7921db8468668c981d89e1f5ea8991e94e67187 (diff) |
Add auto scroll for ToC.
-rw-r--r-- | layouts/partials/toc.html | 6 | ||||
-rw-r--r-- | static/js/toc.js | 16 |
2 files changed, 14 insertions, 8 deletions
diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html index ee6736e..16978d1 100644 --- a/layouts/partials/toc.html +++ b/layouts/partials/toc.html @@ -23,7 +23,11 @@ {{ end }} {{ if gt $next_heading $prev_heading }} {{ range seq (sub $next_heading $prev_heading) }} - <ul class="collapse" data-toggle="collapse"> + {{ if site.Params.enableAutoCollapse }} + <ul class="collapse" data-toggle="collapse"> + {{ else }} + <ul> + {{ end }} {{ end }} {{ end }} {{ $anchorId := (replaceRE ".* id=\"(.*?)\".*" "$1" $header ) }} diff --git a/static/js/toc.js b/static/js/toc.js index 518850a..9ccefbc 100644 --- a/static/js/toc.js +++ b/static/js/toc.js @@ -11,7 +11,7 @@ var spy = function () { var pageBottom = window.pageBottom; var meetUnread = false - var currentIndex = -1 + var lastId = "" elems.forEach(function (elem, idx) { var elemTop = elem.offsetTop; var id = elem.getAttribute('id'); @@ -21,19 +21,21 @@ var spy = function () { return } if (currentTop >= elemTop || currentBottom >= pageBottom) { - navElem.classList.add('toc-active'); } else { if (meetUnread == false) { - meetUnread = true - currentIndex = idx - 1 + meetUnread = true; + try{ + document.getElementById(elems[idx-1].id+"-nav").scrollIntoView({ block:"center", behavior: 'smooth' }); + } catch { + + } + } navElem.classList.remove('toc-active'); } }) - document.querySelectorAll(".collapse").forEach(function(elem){ - elem.classList.remove("collapse"); - }) + } var onNavClick = function () { |