mediawiki-skins-Citizen/resources/scripts/header.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-08-15 17:40:13 +00:00
/*
2019-10-08 19:22:02 +00:00
* Scroll up Header
* Modified from https://codepen.io/sajjad/pen/vgEZNy
* TODO: Convert to Vanilla JS
*/
2019-08-15 17:40:13 +00:00
// Hide header on scroll down
var didScroll;
var lastScrollTop = 0;
2019-10-07 22:53:12 +00:00
var delta = 0;
2019-08-15 17:40:13 +00:00
var navbarHeight = $('.mw-header-container').outerHeight();
2019-10-08 19:22:02 +00:00
$(window).scroll(function(event) {
2019-08-15 17:40:13 +00:00
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make scroll more than delta
2019-10-08 19:22:02 +00:00
if (Math.abs(lastScrollTop - st) <= delta)
2019-08-15 17:40:13 +00:00
return;
2019-10-28 21:46:50 +00:00
// Remove class when header is back in place
if (document.scrollTop == 0) {
$('header').removeClass('nav-down')
} else if (st > lastScrollTop && st > navbarHeight) {
// If scrolled down and past the navbar, add class .nav-up.
2019-08-15 17:40:13 +00:00
// Scroll Down
$('header').removeClass('nav-down').addClass('nav-up');
} else {
2019-10-08 19:22:02 +00:00
// Scroll Up
if (st + $(window).height() < $(document).height()) {
$('header').removeClass('nav-up').addClass('nav-down');
}
2019-08-15 17:40:13 +00:00
}
lastScrollTop = st;
2019-10-08 19:22:02 +00:00
}