Facebook Twitter Gplus LinkedIn RSS

How To Make Your Sidebar Stay Visible When Scrolling

How To Make Your Sidebar Stay Visible on ScrollYou’ll notice as you read the rest of this post that the sidebar follows you as you scroll down the page. I first spotted that effect being used in the Sharebar widget (also seen here), and was surprised to discover how easy it is to do.

It took a bit of research, but I found the necessary code here. However, the example on that forum was for a shopping cart and, for non-technical WordPress users jQuery can be confusing, so I thought I’d give you my Nerd-to-English translation and explain how to easily make your WordPress sidebar stay visible when you scroll…

Getting Started

To keep things simple, I’m going to start with some Basic Instructions that should work for most WordPress themes. Then I’ll touch on Troubleshooting for themes that don’t cooperate right away, and finally I’ll open up the Comments section to your questions.

WARNING: If you are using TwentyTen (the default WP theme) and you modify it, your changes will be lost next time you upgrade WordPress. With any other theme, changes will be lost if you upgrade to a newer version of that theme. For this reason, you should first take the time to learn how to create a child theme.

Basic Instructions

First, go to your wp-admin page and navigate to Appearance > Editor. Along the right side of the screen you’ll see a list of theme files. The file Stylesheet (style.css) should be highlighted; if not, click it.

Scroll all the way to the end of your style.css and add this code:

.fixed { position: fixed; top: 20px; }

Click Update File to save your changes, then click Header (header.php) in your list of theme files.

In header.php, search for this line of code:

<php wp_head(); ?>

If it’s not there, find this instead:

Right above it, insert the following code:

jQuery(document).ready(function($) {
	$(function() {
		if( !(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') ) { // check where the sidebar div is
			var offset = $('#sidebar').offset();
			$(window).scroll(function () {
				var scrollTop = $(window).scrollTop(); // check the visible top of the browser
				if (offset.top < scrollTop) $('#sidebar').addClass('fixed'); else $('#sidebar').removeClass('fixed');
			});
		});
	});
});

Click Update File to save your changes, and you should be done! Check out your website and see if it worked. If not, continue reading…

UPDATE: I have added a conditional statement to the jQuery above, in order to prevent the effect from occurring when your site is viewed on mobile devices. The reason is that, on a mobile device, it is not uncommon for visitors to zoom in on the content body and, when they do, the sidebar would have overlapped the content otherwise.

Troubleshooting

Didn’t work, eh? Not a problem. There are a couple of obvious reasons this won’t work with some themes…

  1. Your sidebar is not identified by the ID #sidebar. This can be solved by editing the jQuery code above and replacing #sidebar with your theme’s sidebar ID (e.g. #widget-container). Note that you need to change it in three places within the jQuery code.
  2. Your theme has jQuery disabled for some reason. Edit your function.php and header.php and search for this: wp_deregister_script(‘jquery’);. If you find it in either place, kill it.
  3. Your sidebar is on the left and your page content is not styled to float right, causing an overlap. Try adding #content { float: right; } to the end of your style.css.

If you still can’t get it working, leave a comment with a link to your site and I’ll try to point you in the right direction.

Questions?

Post them here as comments, along with a link to your site.

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
8 Comments  comments 
  • Design

    Hello, I’m using the Weaver theme, but am very new to WordPress. I can do HTML and CSS pretty well, but thus far I’m stumped on making my sidebars work properly…

    http://accentremodel.com/wordpress/

    Perhaps you can tell me what I’m doing wrong?

    • http://mynewsitepreview.com/ Shaun Scovil

      Hi there! Your sidebar isn’t working because the #content area is a little too wide, has too much right margin, and isn’t floating to the left (thereby allowing your sidebar to float right beside it).

      Try adding this to the very end of your style.css:

      #content {
      float: left;
      margin-right: 0;
      width: 650px;
      }

      Then, if you want your sidebar to stay visible when the user scrolls, follow the instructions above carefully.

      Be sure to replace “#sidebar” with “#primary” in the jQuery code, to make it work with your theme. There are 3 instances of “#sidebar” in the code that you will need to change to “#primary”.

      Good luck! And if you’re ever looking to hire a WordPress developer please keep me in mind…

  • Scott

    I could not get this to work.. through up the jquery code at the top of my page when visiting the website?

    • http://mynewsitepreview.com/ Shaun Scovil

      Shoot me a link and I’ll have a look. Most likely, your sidebar is not identified by the ID #sidebar…

      • Scott

        ok.. thanks.. its a clients site.. I have taken the code out so i could start fresh.. I am using a child theme if that makes a difference also.. http://shadismusic.com when her pages start to extend, she would like the pictures on the sidebar to follow when scrolling..

        Thanks

        • http://mynewsitepreview.com/ Shaun Scovil

          Okay, it looks like you’ve got two sidebar areas (#primary and #secondary, like Twenty Ten). You’re going to want to hide the unused #secondary with `display: none;` to prevent it being used and mucking things up.

          So here is what you should add to your CSS:

          #sidebar {
          display: none;
          }
          .fixed {
          position: fixed;
          top: 20px;
          }

          And here is what you should add to the section:

          jQuery(document).ready(function($) {
          $(function() {
          if( !(navigator.platform == ‘iPad’ || navigator.platform == ‘iPhone’ || navigator.platform == ‘iPod’) ) {

          // check where the sidebar div is
          var offset = $(‘#primary’).offset();
          $(window).scroll(function () {
          var scrollTop = $(window).scrollTop(); // check the visible top of the browser
          if (offset.top < scrollTop) $('#primary').addClass('fixed');
          else $('#primary').removeClass('fixed');
          });
          });
          });
          });

          A better practice would be to put the jQuery code into a .js file and make use of the wp_enqueue_script function, explained here: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

  • TyeNewton

    Hey Shaun,
    How can I make this work with a Thematic child theme?

    I’ve successfully added the javascript by way of its own “scrollfloat.js” file in the head tag, using the wp_head hook:

    function scrollfloat_js() {
    echo ”;
    }
    add_action(‘wp_head’,'scrollfloat_js’);

    I’ve seen that it has been successfully added in the markup.

    I also changed all of the “#sidebar”s to “#primary” which is the correct id for Thematic. Otherwise, the script is exactly as you wrote it, as is the css in my style.css file.

    Also, #content is floated left.

    I wish I could send you a link to my site, but I’m building it locally, using MAMP, so it’s not online yet.

    Hope you have some ideas; thanks!

    • TyeNewton

      Oops, I forgot to come back here and mention that I figured it out. As I remember, I had a couple issues: 1- I didn’t add the jquery source because I was under the impression that it was already loaded into WordPress. 2- Chrome’s Inspector was finding an error in the JavaScript (an extra parenthesis, which I deleted). After that I had to do some finagling with the CSS to make it work for my special case, but it works! Thanks!