blob: ad306a4a6a154ac4869373dde2969f23777e2988 [file] [log] [blame]
Unknownd2db0f12019-01-10 14:22:00 -05001/**
2 * navbar-ontop.js 1.0.0
3 * Add .navbar-ontop class to navbar when the page is scrolled to top
4 * Make sure to add this script to the <head> of page to avoid flickering on load
5 */
6
7(function() {
8
9 var className = "navbar-ontop"
10
11 // we start hidden, to avoid flickering
12 document.write("<style id='temp-navbar-ontop'>.navbar {opacity:0; transition: none !important}</style>")
13
14 function update() {
15 // toggle className based on the scrollTop property of document
16 var nav = document.querySelector(".navbar")
17
18 if (window.scrollY > 15)
19 nav.classList.remove(className)
20 else
21 nav.classList.add(className)
22 }
23
24 document.addEventListener("DOMContentLoaded", function(event) {
25 $(window).on('show.bs.collapse', function (e) {
26 $(e.target).closest("." + className).removeClass(className);
27 })
28
29 $(window).on('hidden.bs.collapse', function (e) {
30 update()
31 })
32 update()
33 // still hacking to avoid flickering
34 setTimeout(function() {
35 document.querySelector("#temp-navbar-ontop").remove()
36 })
37 });
38
39 window.addEventListener("scroll", function() {
40 update()
41 })
42
43})();