Creating an Animated Floating Sidebar With jQuery

Click here for an update to this post using CoffeeScript

I finally got around to implementing a floating sidebar that contains all of my follow links contained in my header. Lots of sites have a floating sidebar, but I wanted mine to be a little different. First, the items in my sidebar come flying out of the header to the sidebar when the user scrolls down past the header, instead of being fixed over on the side. You will also notice a nifty little elastic animation effect.

First let's tackle the fact that the sidebar items actually come "unglued" from the header and hang out over on the side when the user scrolls past the header, and also return to their home inside of the header when scrolled back to the top. The first step is to give the header region containing the home for the follow links relative positioning like so.

Now, all of the magic happens with jQuery. The full code is posted below, but I would like to write you a narrative to explain it. The first step, believe it or not, is to set a timer. We set a timer, because we are going to be binding to the scroll function, and without setting a timeout, you can experience serious performance problems when someone scrolls quickly on your site, the scroll event can be triggered hundreds of times a second with certain mousewheels! The next thing we do is keep track of the home position of the block we will be floating.

It's bind time baby. The very first thing we do inside of the scroll function is to check the timer. If the timer is still going we don't do anything. If the timer has expired we reset the timer and then check if we have scrolled past the home position of our block. If we have, then the first thing is to "unglue" the block by giving it absolute positioning. Then we animate the block over to the side and set it's top position to the top of the window. I have also added easeOutElastic easing to make the animation a little more fun (check out all the types of easing available at this easing types demo). I am also stopping the animation, clearing the queue, and jumping to the end every time the animation is fired, because even with the timer this function will be called a lot and we could potentially have a long queue of animations.

Now, if the user scrolls, and it isn't below the block's home (plus an offset for webkit), then we add the CSS to return the block to it's home. I don't animate it, because I really want the links to be in their home position as soon as the user scrolls up, and doing a really fast animation didn't make sense if the user wasn't going to see the animation anyway. We follow that up by resetting the timer to 0 and then defining the timeout length (250ms in my case, a larger number will have better reliability, but not as smooth of scrolling).

This code is a combination of several methods that I came across while researching how to do a floating sidebar, and I went down several paths before I settled on this code. As a result it is almost surely not optimized (do I need to clear the queue if I am setting a timer anyway?), but it works and should be fairly performant, even in IE. Please leave any suggestions for improvement or any questions in the comments.

jQuery Masonry is Bad Ass

jQuery Masonry is a sweet layout plugin for jQuery. They describe it as "The flip side of CSS floats". It aligns elements vertically then horizontally, minimizing the vertical gaps between elements of varying heights. I created my own sandbox for playing around with masonry.

Like any layout, it has it's time and place. I would consider this primarily an alternative layout, only to be used when trying to be a little different. In terms of usability, one is generally better off sticking to a grid.

If the content being displayed is meant to be "artsy", such as a list of varying sized portfolio pieces or photos, then it is actually really useful and can give your site an "edgy" feel.

On my masonry sandbox page I am also playing around with some other ways to effect Drupal views. In the header of the view I have created buttons that correspond to toggling views field names. This can cause some overlap with the masonry layout that is applied that stacks content on top of other content. However, there is a simple fix; we just add masonry to the callback function of the slideToggle functions to re-mason the page only after the slideToggle has finished firing. The full JS for the page is below.

Drupalcon 2011

Drupalcon Chicago is coming up in about a week! I am super excited to go to my first Drupalcon. Luckily Grand Valley has spring break that week so I don't even have to miss any school. I hope to do a wrap up post on here describing my experience. I am looking forward to learning a lot and meeting some contacts.

I am also looking forward to going to Chicago. I go to Lollapalooza every year and I also have a brother in law in Chicago so I end up going to Chicago 2 or 3 times a year and it is always enjoyable.

Tags: 

Rounded Corners and More! CSS3 PIE Makes Developing for IE Suck Less.

OK, so we all know how much IE sucks. Well I just stumbled across a neat little trick that makes IE suck less. It's called Progressive Internet Explorer (PIE). It's a simple and very effective way to use some advanced CSS features and have them actually work inside of Internet Explorer. PIE supports the following CSS3 features in IE 6-8:

  • border-radius
  • box-shadow
  • border-image
  • multiple background images
  • linear-gradient as background image

Instead of using Javascript and/or images, it uses a 28K .htc file which is loaded simply by uploading the PIE.htc file into your CSS folder and adding the following to your CSS on elements using any of the advanced CSS features

I had been searching for an easy way to do rounded corners in internet explorer for a while before I discovered sweet, glorious PIE. There are many methods involving images and Javascript, and even a few other .htc file implementations, that create rounded corners, but in my opinion they are all junk compared to PIE. PIE has the added benefit that the PIE.htc file is only loaded in IE and only when called from the stylesheet; it doesn't increase the overhead on any of the good browsers!

PIE is an open source project available on github. So if you have the skills, please contribute and let's get all CSS3 behavior rolled into this thing.

Tags: 

Pages

Subscribe to Front page feed