jQuery

Creating an Animated Floating Sidebar With jQuery

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.

Tags: 

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.

Tags: 

Yoxview Lightbox Image Viewer Module for Drupal 7

Yoxview

Yoxview is a wicked rad jQuery lightbox-style image viewer. I recently stumbled across this plugin while looking for a lightbox module for Drupal 7, only to discover that there was no D7 support yet. So I searched on some terms until I found the Yoxview Image Viewer plugin. Unfortunately, the Drupal module is broken. Searching through the issue queue you can find that there is a simple patch that fixes the module for the latest D7 releases. The module is super simple and simply adds yoxview to the displays that you can apply to content fields. There is no configuration options inside of Drupal itself, but all the options for yoxview are easy to configure via javascript.

I added these configuration options directly to the yoxview.js file included in the module. I'm sure there is a better way, but I figured since I had patched the module, I was already altering the module anyway. My yoxview.js file for this site looks like this

Yoxview also contains a ton of gallery options and can even play videos and slideshows. I haven't used many of these options yet. I just love the animation and the functionality of the image viewer.

Have you used Yoxview? What about other lightbox style plug-ins? Do you have an alternative that is currently supported on Drupal 7?

Subscribe to RSS - jQuery