In the project I was working on recently I had a chance to experiment with CSS3 and jQuery. You know CSS3, right? It’s that thing that makes all those beautiful and amazing things on the web and makes Flash want to run away and hide. Yeah, that CSS3. And I’m more than sure you have heard about jQuery. If you haven’t, well, it’s a kind of library where you cannot borrow any books, but it allows you to do other stuff and bring some additional magic to the web.
Anyways, I ended up mixing those two which resulted in this cool effect. I found it really useful and hope you will too, as you can use in on almost every website that has some sort of menu – and let’s face it, most do.
The video below will show you what you’ll be making and how to do that. Unfortunately, since I had only 5 minutes for this, I focused only on the jQuery part. Everything else you need for this you will find in the tutorial below. You can also hit the “
Live Demo” button to see the final effect yourself.
NOTE:
To fully enjoy all of this extravaganza, you need to see it in a browser based on WebKit engine – Safari and Chrome being the best ones. Current version of Firefox doesn’t fully support it, not to mention Opera, or IE.
Table of Contents
- What’s Needed
- All the HTML Stuff
- Importing jQuery and Nagging Menu Script
- Let’s Make a CSS Soup
- CSS3 Extravaganza
- jQuery Magic
- After Word
What’s Needed
Just four things and we’re good to go:
- index.html
- style.css
- nagging-menu.js
- you
HTML markup will be just copy-and-paste thing, as it’s not really important here. Instead, we will dig little deeper into CSS3 and jQuery.
All the HTML Stuff
Create
index.html and paste all of this in:
4 | < title >Create Stay-On-Top Nagging Menu with CSS3 and jQuery</ title > |
5 | < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" /> |
6 | < link rel = "stylesheet" href = "style.css" type = "text/css" media = "screen" /> |
12 | < div id = "menu" class = "default" > |
14 | < li >< a href = "#" >Home</ a ></ li > |
15 | < li >< a href = "#" >CSS</ a ></ li > |
16 | < li >< a href = "#" >Design</ a ></ li > |
17 | < li >< a href = "#" >Development</ a ></ li > |
18 | < li >< a href = "#" >Freebies</ a ></ li > |
19 | < li >< a href = "#" >Inspiration</ a ></ li > |
20 | < li >< a href = "#" >Resources</ a ></ li > |
21 | < li >< a href = "#" >Tutorials</ a ></ li > |
22 | < li >< a href = "#" >WordPress</ a ></ li > |
29 | Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
33 | Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
37 | Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
Depending on your screen resolution, you will need a lot of text so…
IMPORTANT: Keep adding paragraphs with “lorem ipsum” until the menu disappears from your sight entirely when you scroll down to the very bottom of the page.
Importing jQuery and Nagging Menu Script
In your
index.html file, just before the closing
</body>
tag, paste those two lines:
2 | < script type = "text/javascript" src = "nagging-menu.js" charset = "utf-8" ></ script > |
Bravo ;D!
Let’s Make a CSS Soup
This is just the basic CSS, so create
style.css file and paste all of this in:
7 | font : normal 13px Verdana , sans-serif ; |
36 | list-style-type : none ; |
43 | text-decoration : none ; |
The
.default
and
.fixed
classes are very important to us. They will change the look and position of our menu; the former is responsible for how the menu looks at the beginning and for its position in the browser window, the latter is responsible for where the menu goes after we kick it out from its original position and how it looks then.
CSS3 Extravaganza
Now for the fun part :)! Let’s go from the top to the bottom through our CSS soupe and add some CSS3 sugar accordingly:
Step 1: Spicing Up #menu
We’ll add a subtle gradient and rounded corners.
The gradient for browsers based on
WebKit
engine:
1 | background : -webkit-gradient(linear, left top , left bottom , color-stop( 0% , #426d9c ), color-stop( 40% , #0f67a1 ), color-stop( 100% , #1384d1 )); |
Simple as it is. We’re adding linear gradient, going from the top to the bottom vertically. Our gradient consists of three colors,
color-stop
simply tells the browser where that particular color should stop and the next one begin.
The gradient for browsers based on
Gecko
engine:
1 | background : -moz-linear-gradient( top , #426d9c , #0f67a1 , #1384d1 ); |
The same as above, only we don’t have to be specific about where each color stops, because it looks great as it is already.
Let’s add rounded corners, first for browsers with
WebKit
engine:
1 | -webkit-border-radius: 5px ; |
That’s really self explaining. The syntax for browsers with
Gecko
engine looks like this:
1 | -moz-border-radius: 5px ; |
And at last, the proper syntax:
Your CSS3 styles for
#menu
should look like this right about now:
2 | background : -webkit-gradient(linear, left top , left bottom , color-stop( 0% , #426d9c ), color-stop( 40% , #0f67a1 ), color-stop( 100% , #1384d1 )); |
3 | background : -moz-linear-gradient( top , #426d9c , #0f67a1 , #1384d1 ); |
5 | -webkit-border-radius: 5px ; |
6 | -moz-border-radius: 5px ; |
Step 2: Spicing Up ul li a
Here, let’s add rounded corners, text shadow and some subtle transition. We know the syntax for rounded corners already:
1 | -webkit-border-radius: 5px ; |
2 | -moz-border-radius: 5px ; |
Now, the shadow for our links:
1 | text-shadow : 1px 1px 1px #000 ; |
This will place the shadow 1px to the right and below our link. The shadow will have 1px blur radius and black color.
Now, our smooth transition:
1 | -webkit-transition-property: color, background; |
2 | -webkit-transition-duration: 0.5 s, 0.5 s; |
The
transition-property
determines which properties fall under the transition, in our case it will be
color
along with
background
. The
transition-duration
determines how long the transition should last, it will be
0.5s for
color
and
0.5s for
background
accordingly.
Your CSS3 styles for
ul li a
should look like this:
2 | text-shadow : 1px 1px 1px #000 ; |
4 | -webkit-border-radius: 5px ; |
5 | -moz-border-radius: 5px ; |
8 | -webkit-transition-property: color, background; |
9 | -webkit-transition-duration: 0.5 s, 0.5 s; |
Step 3: Spicing Up ul li a:hover
Nothing new here, CSS3-wise:
2 | -webkit-transition-property: color, background; |
3 | -webkit-transition-duration: 0.5 s, 0.5 s; |
Step 4: Spicing Up .default
Here we’ll add only box shadow:
1 | -webkit-box-shadow: 0 5px 20px #888 ; |
2 | -moz-box-shadow: 0 5px 20px #888 ; |
3 | box-shadow: 0 5px 20px #888 ; |
Similiar to
text-shadow
, this will place the shadow 0px to the right and 5px below our menu. The shadow will have 20px blur radius and
#888
color.
Step 5: Spicing Up .fixed
The same goes for
.fixed
, we’ll add only box shadow:
1 | -webkit-box-shadow: 0 0 40px #222 ; |
2 | -moz-box-shadow: 0 0 40px #222 ; |
3 | box-shadow: 0 0 40px #222 ; |
This time it will place the shadow 0px to the right and 0px below our menu, but the shadow will have 40px blur radius and
#222
color.
The
entire file should look like this right now:
7 | font : normal 13px Verdana , sans-serif ; |
12 | background : #0F1620 url (bg.jpg) no-repeat top center ; |
15 | border : solid #0F1620 ; |
16 | border-width : 3px 0 3px 0 ; |
25 | background : -webkit-gradient(linear, left top , left bottom , color-stop( 0% , #426d9c ), color-stop( 40% , #0f67a1 ), color-stop( 100% , #1384d1 )); |
26 | background : -moz-linear-gradient( top , #426d9c , #0f67a1 , #1384d1 ); |
28 | -webkit-border-radius: 5px ; |
29 | -moz-border-radius: 5px ; |
51 | list-style-type : none ; |
58 | text-decoration : none ; |
61 | text-shadow : 1px 1px 1px #000 ; |
63 | -webkit-border-radius: 5px ; |
64 | -moz-border-radius: 5px ; |
67 | -webkit-transition-property: color, background; |
68 | -webkit-transition-duration: 0.5 s, 0.5 s; |
75 | -webkit-transition-property: color, background; |
76 | -webkit-transition-duration: 0.5 s, 0.5 s; |
83 | -webkit-box-shadow: 0 5px 20px #888 ; |
84 | -moz-box-shadow: 0 5px 20px #888 ; |
85 | box-shadow: 0 5px 20px #888 ; |
94 | -webkit-box-shadow: 0 0 40px #222 ; |
95 | -moz-box-shadow: 0 0 40px #222 ; |
96 | box-shadow: 0 0 40px #222 ; |
Great job, we’re done with CSS. Let’s add some jQuery!
jQuery Magic
All our code should be executed after the
DOM
is loaded. For that we’ll use this great method jQuery has, called
.ready()
. You’re probably familiar with the long notation:
1 | $(document).ready( function () { |
But this time let’s use the short notation, which does exactly the same thing:
Now, first thing we should do, is to grab our menu (which is a
div
with ID “menu”) and assign it to a variable, because we’ll use it several times. The same goes for its position, and for that we’ll use jQuery’s
.offset()
method which, unlike
.position()
method, returns the position of the element we use it on relatively to the document, not to the offset parent. Let’s do it:
Next, we need to listen for the scroll event, so every time we scroll the page up or down something happens:
1 | $(window).scroll( function (){ |
Now we need to check if we scrolled down so far that our menu is not visible anymore. This point is exactly the position of our menu from the top + its height. We also need to check if the menu is now in its default position and has the default look:
1 | if ($( this ).scrollTop() > pos.top+menu.height() && menu.hasClass( 'default' )){ |
If the result of our
if
statement is true – we want the menu to fade out, the we need to remove the
.default
class, give it a class of
.fixed
and then fade it back in, so we can see it:
1 | menu.fadeOut( 'fast' , function (){ |
2 | $( this ).removeClass( 'default' ).addClass( 'fixed' ).fadeIn( 'fast' ); |
Right now the effect works only in 50%; it fades out when you scroll down and then it reappears at the top of your browser window – but it doesn’t return to its original position and size when you scroll back up. Let’s fix it now.
For this we need another
if
statement. This one will be the exact opposite of the first one we made, so:
1 | else if ($( this ).scrollTop() <= pos.top && menu.hasClass( 'fixed' )){ |
If the result of our statement is true, we want the menu to fade out again, remove the
.fixed
class and give it a class of
.default
this time, and then we want it to fade back in, so it’s visible to us:
1 | menu.fadeOut( 'fast' , function (){ |
2 | $( this ).removeClass( 'fixed' ).addClass( 'default' ).fadeIn( 'fast' ); |
The script is complete now, this is how it should look:
6 | $(window).scroll( function (){ |
7 | if ($( this ).scrollTop() > pos.top+menu.height() && menu.hasClass( 'default' )){ |
8 | menu.fadeOut( 'fast' , function (){ |
9 | $( this ).removeClass( 'default' ).addClass( 'fixed' ).fadeIn( 'fast' ); |
11 | } else if ($( this ).scrollTop() <= pos.top && menu.hasClass( 'fixed' )){ |
12 | menu.fadeOut( 'fast' , function (){ |
13 | $( this ).removeClass( 'fixed' ).addClass( 'default' ).fadeIn( 'fast' ); |
Well done! Now, go and implement it somewhere and have fun :).
After Word
With a little customization you can easily tweak the whole thing to your liking and/or need, which makes this little effect very useful. Combine it with CSS3 and you’re done :). That’s it guys! If you have any questions, feel free to ask in the comments, I’ll do my best to answer them. Cheers!