December 28, 2024
1 min read
Essential Code Snippet For Web Developers

With this simple jQuery will add a class on the header to show. We will need some CSS and jquery to make it happens.

HTML markup will be something like that..

Index.html
<div class="header_area">
    <div class="logo_wrapper">
        <h1>Title</h1>
    </div>
    <div class="navigation">
        <nav class="nav-collapse">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </div>
</div>

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

Custom.js
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.

Custom.js
$(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

CSS for hide header when scrolling down
.header_area.sticky_top {
  position: fixed;
  width: 100%;
  top: 0;
  background: #000;
  color: #fff;
  z-index: 99;
  padding: 10px 0;
  opacity: 0;
  visibility: hidden;
}
After go down and scrolling up
.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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Random slide order in slick sider

Change slick carousel slide order randomly

Most View or Popular Post on WordPress

WordPress Post query by user views without plugin and simple code snippet

WooCommerce Custom Order Dropdown Based on Payment Method

WooCommerce admin order page custom drop-down base on payment method & show order admin column with code only

JS fetch post API Data in 5 min simple way

js fetch post API is very simple way. some line of Javascript code and boom.

Web Development Project in mind?

if you looking for a web developer for paid contribution to your project I am available for work.

Mukto
Mukto

Click the button below to chat with me.