Site logo

Archived topic

It is possible to use sticky header without jquery?

5 replies · Started by Fabio on December 17, 2020

Viewing posts 1–6 of 6

Hi all i just understood that sticky navigation requires jquery to work, but in my website i disabled jquery and i would like to know if it is possible to use fixed navigation even without usign Jquery, i dont't know, with Vanilla or Css or what esle
thank you

Hi there,

its not possible to do the transition with CSS alone, it would require some JS to do that. We will be replacing jQuery in the next update with Vanilla JS.

temporary solution:
<script>
(function(){

var doc = document.documentElement;
var w = window;

var prevScroll = w.scrollY || doc.scrollTop;
var curScroll;
var direction = 0;
var prevDirection = 0;

var header = document.getElementById('masthead');

var checkScroll = function() {

/*
** Find the direction of scroll
** 0 - initial, 1 - up, 2 - down
*/

curScroll = w.scrollY || doc.scrollTop;
if (curScroll > prevScroll) {
//scrolled up
direction = 2;
}
else if (curScroll < prevScroll) {
//scrolled down
direction = 1;
}

if (direction !== prevDirection) {
toggleHeader(direction, curScroll);
}

prevScroll = curScroll;
};

var toggleHeader = function(direction, curScroll) {
if (direction === 2 && curScroll > 50) {

header.classList.add('hide');
prevDirection = direction;
}
else if (direction === 1) {
header.classList.remove('hide');
prevDirection = direction;
}
};

window.addEventListener('scroll', checkScroll);

})();

</script>

<style>
body{padding-top:50px}
#masthead {
position: fixed;
height: 50px;
top: 0px;
width: 100%;
z-index: 100;
transition: all .3s ease;

}
#masthead.hide {
top: -51px;
}
</style>

You're welcome - and thanks for sharing your solution :)

This archived topic is closed to new replies.