Change header as scrolling the page

Oct 062013
 
For using this solution on simple html sites, without WordPress and Suffusion, see my answer on this question from Stack Overflow. For seeing the codes in action and testing the solution for your own site, check this fiddle on jsfiddle.net.
Latest Edit: I changed the hide() and show() methods for #left-header-widgets used for displaying/hiding the Left Header Widget Area which contain the small logo, with css(‘visibility’,…). The CSS visibility property will hide the element but will keep its container, so with the actual code the menu will stay in the same position either when scroll or not,

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. 

Changing header as scrolling
Changing header as scrolling

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)

 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:

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.

https://drafie-design.nl/change-header-as-scrolling-the-page/trackback/Trackback URI:  

Ciprian

Experienced web developer and graphic designer. Former consultant for Suffusion theme and former moderator on Suffusion Support forum. I teach others while learning myself.

Google+ Profile

    Google+ Page

 Leave a Reply

(Your comment will be published. To contact me privately, use the contact form.)

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  19 Responses to “Change header as scrolling the page”

Comments (18) Pingbacks (1)
  1. Hi drake,

    I try to implement this how to http://www.drafie-design.nl/change-header-as-scrolling-the-page/ on http://pitsaja.com, the header is okay but the footer become hidden/unreadable. How to fix this. Thank for your reply.

     
  2. Thank you.. works like a charm!!

     
  3. Hi, thanks for coming and apologize for my delayed answer. I saw your comments but I was very busy these days and I haven’t time to research on your problems.

    I always disable wp-admin bar and “edit” link in pages as I want to see my sites exactly as my visitors. I always keep opened 2 tabs when I work on a site – the admin area in one tab where I make changes, and the frontend in another tab to see the changes in action. The code need a “fixed” position for staying in the same place when scroll the site. If you always keep the admin bar on top, even for visitors, is simple to change the top position of .scroll-nav element (top:32px !important; should do the trick).

     
  4. The code is not longer active on your site, but I can presume that the header was smaller with the equivalent of padding (so, padding:0; for #header element should help). And the line was the bottom border of #nav element which can be removed at Suffusion Options -> Skinning -> Navigation Bar Below Header -> Navigation Bar Border, or by adding at Custom Styles:

     
  5. Hello,

    i am using a rotating set of background images for my suffusion theme. To get the background images fit in the container when using responsive settings I had to place #header-container {background-size:contain;} in my style file.

    Then the header image fitted correctly. But when i try to use this code of your site something strange is happening.

    I had to remove the width:100%; /* this will re-align the header with the wrapper */ part and replace it with background-size:contain;to get almost the right size.

    then I had to comment out the left:0; /* align horizontally the header in IE if is needed */ te get it in the middle of the site.

    So now the header is above the main site, staying if scrolling so thats cool but the header area is half a cm smaller on both sides of the site.

    And below the navigation menu there is a blue line with 100% width of my total window.

    How to solve this?

    thank you!

    jap

     
  6. Hello,

    Nice code. But the menu will stay behind the admin/buddypress bar. Is it possible to show it under the admin bar?

    thank you in advantage!

     
  7. I edited the codes in the original post to reflect the changes for IE browsers (http://www.drafie-design.nl/how-to-have-a-fixed-header-in-suffusion-theme/)

     
  8. Ok Michael, in the same idea replace in my code “margin-top” with “top” property, so will have:

     
  9. Thanks! That worked great for the Nav Width settings in IE10 (by the way it was already working well in Android as well).

    Now the only remaining challenge is IE10 not responding to #wrapper {
    position:relative;
    margin-top:130px;
    }

     
  10. Hello Drake. Your code for Sticky Header has worked great for me in Firefox and Chrome – Thanks a lot! Of course IE10 isn’t being as cooperative. It’s not responding to the Nav Width settings nor the Wrapper Margin-Top. Is there anything you can do to whip it into submission? http://michaelmaguireconsulting.com

     
  11. I can’t test, because I don’t have IE10 anywhere, but, try to add also at Custom Styles:

     
  12. Thank you for pointing out to me over in the Suffusion forum that I had missed this response! I must have not ticked the ‘notify me’, so that I didn’t get an email. I should have known to check back here :/

    Still needs tweaking: my small logo isn’t centered well. Did you just use an image widget to add the small header?

     
  13. No, the changes will work even if you have widgets in Header Widget Area, the javascript already address that area (identified by id ‘#header-widgets’).

    But, you made a mistake when you’ve added the codes from above. You added ALL codes at Custom Footer Javascript, while there is necessary to add only the first piece of code. The second piece is CSS and need to be added at Suffusion Options -> Backend -> Custom Includes -> Custom Styles (the first edit area from that options page).

    Need to respect exactly the steps as described and the places where need to paste the codes. A CSS code pasted in place for javascripts will break not only your custom javascripts, but even the javascripts used by theme or plugins. As jQuery functions pasted in the place reserved for CSS will break the look of entire site.

     
  14. Tried to make this change at http://www.atelieremmanuel.org, but nothing happened. Maybe because I have widgets activated in my header?

     
  15. Oooops…Please ignore my last comment as I couldn’t get it to work becauseI had a typo. Thanks for the post!

     
  16. Instead of using a background colour I’d like to use a background-image. I can’t seem to get this to work as it scrolls away regardless of what I try. Any thoughts? Thanks a lot!

     
  17. Can try to change the value of 150 in the jQuery code. Test what’s happens if increase/decrease the value. Also can try to fix the height of header container by adding at Suffusion Options -> Backend -> Custom Includes -> Custom Styles:

    #header-container {height:130px;}

    (also can play with the value of 130px).
    As I said, I didn’t changed anything which was previously added for fixing the header, so, my dimensions are already fixed as in previous article. But even in that article I specified that some positions and dimensions need to be adjusted for each site.

     
  18. Thanks for this post. Navigation bar fixed nicely, but when we scroll the page it hides some part of the post instantly. Please have a look on my site http://infogyd.com