How we can create a sticky header with a smooth transition? Solution: CSS Sticky Header Transition With jQuery, Animated Header Section Stick.
Previously I have shared a header underline program, but this is a sticky header on scroll program. Basically, The <header> element contains website branding, navigation elements, search forms, and similar content of a website. Most of the time I shared <nav> tag for creating navigations or navbar, but it always placed inside the header. In other words, the header tag defines the header of the document. We can create a sticky header to create proper navigation.
Today you will learn to create Animated Header Section Stick. Basically, there is a header section with a text, and a nav tag with some navigation items. Also, there are 3 sections of dummy text to create a scrollable webpage. When you will scroll down, then the header text will hide with an animation effect and the navbar will stick on the top of the webpage. And again when you will scroll top then the header section will reveal again.
So, Today I am sharing CSS Sticky Header Transition With jQuery. There I have used HTML to create the layout, CSS for styling and animation, jQuery for functioning. If you want to create a single-page website, then you can make this program by modifying it. Also, you can apply this effect on your website by using the CSS codes.
If you are thinking now how this sticky header animation actually is, then see the preview given below.
Preview Of Animated Header Section
See this video preview to getting an idea of how this animated header looks like.
Now you can see this program visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
CSS Sticky Header Transition Source Code
Before sharing source code, let’s talk about it. First I have created a header section using HTML <header> tag and inside it, I have placed a heading text and a <nav> tag. The nav section contains 4 items for the navbar links. After that, I have created a div named ‘wrapper’ and placed 3 sections inside it with dummy text. Also in the HTML file, I have linked other files like CSS, JS, jQuery.
Now using CSS I have placed all the items in the right place, as you can see in the preview. With CSS first I gave basic values like size, position, margin, padding, etc to all the elements. To create the text hide animation effect I have used CSS transform command and for smooth animations used transition command. The CSS codes are applied directly to tags, not class or ID, so you can use them anywhere easily.
jQuery handling here the add and remove class on scroll feature in this program. There I have used .scrollTop command to detect the scrolling and if{} else{} statements to declare the conditions. Left all other things you will understand after getting the codes, you can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second for CSS, and third for JavaScript.
Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Sticky Header CSS Transition | Webdevtrick.com</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1>Sticky Header Pow!</h1> <nav> <a href="#">Home</a> <a href="#">About</a> <a href="#">Gallery</a> <a href="#">Contact</a> </nav> </header> <div class="wrapper"> <section id='steezy'> <h2> This is some steezy stuff</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ex libero cupiditate deserunt earum, fuga, ab labore esse animi, nihil est tenetur non delectus perferendis. Maxime accusamus ea rem sint at!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum! Lorem ipsum dolor sit amet, consectetur adipisicing elit. A ut aspernatur omnis neque iusto. Veritatis rem ad atque optio perferendis. Dolorum qui consequatur, repellat soluta impedit porro totam molestiae sit.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Possimus iusto, omnis quas, rem minus cupiditate, sunt voluptates minima sint nemo ipsa itaque consequatur dolor, quam necessitatibus vitae. Quod, eaque, quam.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> </section> <section id='real'> <h2> Keepin' it real</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> </section> <section id='big'> <h2>Go big, or go home</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum enim sunt delectus, reiciendis omnis laudantium animi, et nulla quidem, aliquam aliquid. A consectetur deleniti voluptates eius neque, voluptate voluptatibus nostrum!</p> </section> </div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ * { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; margin: 0; padding: 0; border: 0; font: inherit; font-size: 100%; vertical-align: baseline; } html { line-height: 1; } ol, ul { list-style: none; } table { border-collapse: collapse; border-spacing: 0; } caption, th, td { text-align: left; font-weight: normal; vertical-align: middle; } q, blockquote { quotes: none; } q:before, q:after, blockquote:before, blockquote:after { content: ""; content: none; } a img { border: none; } article, aside, details, figcaption, figure, footer, header, hgroup, main, menu, nav, section, summary { display: block; } body { background-color: #ecf0f1; font-family: helvetica, arial, sans-serif; font-size: 16px; padding-top: 330px; -moz-transition: padding-top 0.5s ease; -o-transition: padding-top 0.5s ease; -webkit-transition: padding-top 0.5s ease; transition: padding-top 0.5s ease; } header { width: 100%; height: 300px; background-color: #3498db; text-align: center; position: relative; position: fixed; top: 0; overflow: hidden; -moz-transition: all 0.5s ease; -o-transition: all 0.5s ease; -webkit-transition: all 0.5s ease; transition: all 0.5s ease; } header h1 { font-size: 42px; color: #fff; line-height: 230px; text-transform: uppercase; font-weight: 100; -moz-transition: all 0.3s ease; -o-transition: all 0.3s ease; -webkit-transition: all 0.3s ease; transition: all 0.3s ease; } header nav { position: absolute; bottom: 0; height: 60px; line-height: 60px; width: 100%; background-color: rgba(0, 0, 0, 0.1); } header nav a { color: #fff; display: inline-block; padding: 10px 15px; line-height: 1; text-decoration: none; -moz-border-radius: 2px; -webkit-border-radius: 2px; border-radius: 2px; } header nav a:hover { -moz-box-shadow: 0 0 0 1px #fff; -webkit-box-shadow: 0 0 0 1px #fff; box-shadow: 0 0 0 1px #fff; } h2 { font-size: 34px; text-transform: uppercase; font-weight: 100; line-height: 2; color: #2c3e50; } p { margin-bottom: 2rem; line-height: 2; color: #7f8c8d; } .wrapper { width: 800px; max-width: 100%; margin: 0 auto; } section { padding: 20px; margin-bottom: 40px; background-color: #fff; -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; -moz-box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2); -webkit-box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2); } body.sticky-header { padding-top: 100px; } body.sticky-header header { height: 60px; background-color: rgba(52, 152, 219, 0.9); } body.sticky-header header h1 { -moz-transform: scale(0, 0); -ms-transform: scale(0, 0); -webkit-transform: scale(0, 0); transform: scale(0, 0); } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 |
// Code By Webdevtrick ( https://webdevtrick.com ) $(function(){ $(window).scroll(function(){ var winTop = $(window).scrollTop(); if(winTop >= 30){ $("body").addClass("sticky-header"); }else{ $("body").removeClass("sticky-header"); }//if-else });//win func. });//ready func. |
That’s It. Now you have successfully created CSS Sticky Header Transition With jQuery, Animated Header Section Stick. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.