Change header as scrolling the page
Some time ago I wrote this article about how can fix the header in Suffusion theme. Few days ago it was a question on that article about how to fix the Navigation Bar Below Header, but only after the page is scrolled.
So, here is the answer – it’s pretty simple to obtain that in Suffusion. I don’t change anything from that article about fixed header, only I added few lines of javascript and few lines of CSS. But this new codes need to work even on a clean installation of Suffusion, without fixing the entire header as is shown in the previous post. Also, a simple way to have a fixed menu is to use the Navigation Bar Above Header which can be fixed as it shown in that post.
But for large headers is better to use the Navigation Bar Below Header, as it keep the menu in under the eyes of visitors. So, below are my additions for fixing the Navigation Bar Below Header after the user scroll down the page.
1. First I added the next code at Suffusion Options -> Back-end -> Custom Includes -> Custom Footer Javascript (can be also added at Custom Header Javascript, but there I have some other functions and I wish to show you only this piece of code)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$j=jQuery.noConflict(); $j(document).ready(function() { var nav = $j('#nav'); $j('#left-header-widgets').css('visibility','hidden'); $j(window).scroll(function () { if ($j(this).scrollTop() > 150) { nav.addClass("scroll-nav"); $j('#header').hide(); $j('#header-widgets').hide(); $j('#left-header-widgets').css('visibility','visible'); } else { nav.removeClass("scroll-nav"); $j('#header').show(); $j('#header-widgets').show(); $j('#left-header-widgets').css('visibility','hidden'); } }); }); |
What does the code? The code measure the scrolling amount from the top of page. When the user scroll down with more than 150px (can change this value as you wish), more events will happens.
- we add a new class to navigation bar: .scroll-nav – this new class will be used for positioning the navigation bar on the top of page after scrolling;
- we hide the #header and #header-widgets elements, we don’t need them after scrolling down;
- but we display the #left-header-widgets where we’ve previously added a smaller version of logo, so our logo will be displayed in the navigation bar.
- when the user scroll back to top, the header reappear.
Now, few lines of CSS added at Suffusion Options -> Back-end -> Custom Includes -> Custom Styles will definitivate our changeable header:
1 2 3 4 |
.scroll-nav {z-index: 9999; position: fixed; left: 0; top: 0 !important; width: 100%;} #nav #left-header-widgets a {background:none;} #left-header-widgets {visibility:hidden;border:none;} #left-header-widgets img {border:none !important;} |
The first line it format our new class added by jQuery code, so the navigation bar is moved on the top of page. The rest of lines format the smaller logo added at Appearance -> Widgets in Left Header Widgets Area.
What can I say more? It’s done… Aaa… I centered the navigation bar at Suffusion Options -> Other Graphical Elements -> Navigation Bar Below Header for making room for smaller logo. Can see the result right now if you look on the top of this page.
[…] Bar Below Header and change the header AFTER the user scroll down the page, check also this post: http://www.drafie-design.nl/change-header-as-scrolling-the-page/. I changed the header of this site as is instructed in that […]