on gh-pages help sign in
searx/_static/version_warning_offset.js
40 lines 1.6 kB history raw
1/*
2When showing the sticky version warning, the warning will cover the
3scroll target when navigating to #id hash locations. Take over scrolling
4to adjust the position to account for the height of the warning.
5*/
6$(() => {
7 const versionWarning = $('.version-warning')
9 // Skip if there is no version warning, regular browser behavior is
10 // fine in that case.
11 if (versionWarning.length) {
12 const height = versionWarning.outerHeight(true)
13 const target = $(':target')
15 // Adjust position when the initial link has a hash.
16 if (target.length) {
17 // Use absolute scrollTo instead of relative scrollBy to avoid
18 // scrolling when the viewport is already at the bottom of the
19 // document and has space.
20 const y = target.offset().top - height
21 // Delayed because the initial browser scroll doesn't seem to
22 // happen until after the document ready event, so scrolling
23 // immediately will be overridden.
24 setTimeout(() => scrollTo(0, y), 100)
25 }
27 // Listen to clicks on hash anchors.
28 $('a[href^="#"]').on('click', e => {
29 // Stop default scroll. Also stops the automatic URL hash update.
30 e.preventDefault()
31 // Get the id to scroll to and set the URL hash manually.
32 const id = $(e.currentTarget).attr('href').substring(1)
33 location.hash = id
34 // Use getElementById since the hash may have dots in it.
35 const target = $(document.getElementById(id))
36 // Scroll to top of target with space for the version warning.
37 scrollTo(0, target.offset().top - height)
38 })
39 }
40})