In my html page I have a table of contents with CSS attribute position: fixed;
and would like to highlight (bold or italics) the current reading/displaying position there while scrolling down the page.
| yada yada yada ...
1. Section 1 |
1.1 Subsection 1 | 1.2 Subsection 2
1.2 Subsection 2 <-- bold |
2. Section 2 | Lorem ipsum dolor sit amet,
[...] | consectetur adipisici elit
| [...]
I would like to keep it simple and CSS -- if at all possible -- would be preferred over JS. The site is being generated by Hugo but the answer doesn't necessarily need to be Hugo specific.
Thanks a lot!
P.s. In case of a Hugo specific answer: The toc is being generated using {{ partial "table-of-contents" . }}
<!-- ignore empty links with + -->
{{ $headers := findRE "<h[1-6].*?>(.|\n])+?</h[1-6]>" .Content }}
<!-- at least one header to link to -->
{{ $has_headers := ge (len $headers) 1 }}
<!-- a post can explicitly disable Table of Contents with toc: false -->
{{ $show_toc := (eq $.Params.toc true) }}
{{ if and $has_headers $show_toc }}
<div class="table-of-contents toc bd-callout">
<!-- TOC header -->
<h4 class="text-muted">Table of Contents</h4>
{{ range $headers }}
{{ $header := . }}
{{ range first 1 (findRE "<h[1-6]" $header 1) }}
{{ range findRE "[1-6]" . 1 }}
{{ $next_heading := (int .) }}
<!-- generate li array of the proper depth -->
{{ range seq $next_heading }}
<ul class="toc-h{{ . }}">
{{end}}
{{ $base := ($.Page.File.LogicalName) }}
{{ $anchorId := ($header | plainify | htmlEscape | urlize) }}
{{ $href := delimit (slice $base $anchorId) "#" | string }}
<a href="{{ relref $.Page $href }}">
<li>{{ $header | plainify | htmlEscape }}</li>
</a>
<!-- close list -->
{{ range seq $next_heading }}
</ul>
{{end}}
{{end}}
{{end}}
{{ end }}
</div>
{{ end }}