Floating sidebar with jQuery and WordPress

Recently I just came across an awesome plugin that floats a sidebar or any element along as the page scrolls without overlapping any parent elements. So today I figured I would write a tutorial on how to take advantage of this plugin and use it in one of your WordPress themes.

DEMO

NOTE: I used WPCandy’s sample posts and data to extend the page and show off the plugin

The first step to getting this working is to add the jQuery library onto your theme the easiest way of doing this is to go inside of your header.php file inside wp-content and add this right above wp_head();

1
wp_enqueue_script('jquery');

After adding that code I usually use Firebug to check the head tag and make sure jQuery is loaded properly. Now we load the stickyFloat plugin into the header.php file right under wp_head(); but outside of the php tags.

1
<script type="text/javascript" src="<?php bloginfo('template_directory');?>/js/floatBox.js"></script>

Right under that we want to initiate the plugin and choose what element we want to allow to scroll

1
2
3
4
5
<script type="text/javascript">// <![CDATA[
jQuery(document).ready(function(){
  jQuery('#primary').stickyfloat({ duration: 800 });
});
// ]]></script>

NOTE: Duration is the delay it takes for the element to scroll to where you are on the y-axis of the document

 

After that’s all done now we need to edit the CSS to get it fully functional. The way the plugin works the element needs to have absolute positioning to work because as the page scrolls the plugin is increasing or decreasing the element’s top position value to move where we are on the y-axis of the document. The element also needs a relative positioned parent to know how far down or up it can scroll without overlapping or going under the footer or header.

 

So to get started lets open the style.css file (I’m using the Twenty Ten theme that comes with WordPress so it may be a little different for you if your using a different theme). Next we find the element we are scrolling in my case it’s the #primary div. Here is what i added.

1
2
3
4
5
#primary {
float: none;
height: 650px;
position: absolute;
right: 0; /* moves sidebar to original position */

I also had to add position:relative; to the #main div which is the parent element of all the content in the middle of the page and the sidebar. Now onto the last part open your stickyfloat javascript file and find and replace “$” with “jQuery” this will get it to work with WordPress this is a very dirty and quick way of doing it and you can manually do it if you know what your doing and want to. This is needed because WordPress already has scripting libraries that use “$” and fails to call jQuery commands so using jQuery is an alternative way for getting it to work for another way check out my previous post on it here.

stickyfloat_files.zip

6 Responses to Floating sidebar with jQuery and WordPress

  1. Adam says:

    Hi,

    I’m wondering where I find my “stickyfloat javascript file”?

    Is it a file on my websites server? Was I meant to download it and put it on my server?

    Any help would be greatly appreciated.

    I’m trying to implement this in the same theme: twentyten and have the latest wordpress and theme install.

    • Adam says:

      I found the file, its not available anymore on the original site but I found it at https://gist.github.com/728487 I hope it works the same.

      I modified this line to be:

      Is that correct?

      Also, I think I’ve stuffed my style.css file up. Would the modified style.css file from your practice site work in my site? Assuming I modified the directory of the stickyfloat file?

      It would be of great help if you were able to email me the style.css file from your practice site as I’m using the same theme and am unsure which #primary and #main to add what you said (as there are multiple #primary). My email is aqwertydude@hotmail.com

      I’m new to this and your tutorial has by far been the easiest to follow out of others I have found for this implementation however, I’m still having some troubles.

      Regards

  2. admin says:

    Yeah I can upload the files. I also had problems with one of the stickyFloat.js files had to get a newer version it wasn’t working on some browsers. Yes it should work if it doesn’t you probably just have to delete a few things from my stylesheet. Thanks I plan on posting more tutorials and possibly video tutorials soon :D

  3. [...] thing to do is enable jQuery inside WordPress I posted this on my blog here If your not sure how to do it already with enqueing jquery. After that I chose to load jquery ui [...]

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>