let css = '';
for (let i = 1; i <= 999; i++) {
css += `.z-${i} {\n z-index: ${i};\n}\n`;
}
console.log(css);
Here on HTML we have header_area Class. We will add a new Class when the user
scrolls up. The class will only add when user trying to go up after scroll down to the
page
var prevScrollpos = window.pageYOffset;
window.onscroll = function () {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
$(".header_area").addClass('sticky');
} else {
$(".header_area").removeClass('sticky');
}
prevScrollpos = currentScrollPos;
}
Now we have a sticky class with our header_area
. But we need to hide Header when scrolling down. To do that we need to add one more class to we can hide when scroll down.
$(window).scroll(function() {
if ($(this).scrollTop()) {
// add class when scroll down
$(".header_area").addClass("sticky_top");
} else {
//Remove Class when header stay at top
$(".header_area").removeClass("sticky");
$(".header_area").removeClass("sticky_top");
}
});
We just added sticky_top calls with our header when it go scroll down. And remove all
two classes that we added last time for scroll down to scroll up.
We are done now just use your own css show it like as you want. I will give you some as
example
.header_area.sticky_top {
position: fixed;
width: 100%;
top: 0;
background: #000;
color: #fff;
z-index: 99;
padding: 10px 0;
opacity: 0;
visibility: hidden;
}
.header_area.sticky_top.sticky{
opacity: 1;
visibility: visible;
}
That’s the all. Hope it helps. Comment below if you have any issue with this code.
2 Comments.
Lija akter
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which.
Tanim Mahbub
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which.
Israt Sultana
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which.
Mahidul Islam
Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque quia pariatur consectetur autem, temporibus aperiam molestiae debitis reiciendis, praesentium asperiores deleniti illum in alias vitae, doloribus impedit ex molestias officiis!