scriptygoddess

01 Feb, 2009

jQuery Side Scroll

Posted by: Jennifer In: jquery

(This post has been almost completely re-written. Usually I don't that – but I thought it would be more confusing to have all the changes, strikeouts, etc.)

For a project I'm going to be working on, I needed to create a navigation piece that had different panes scrolling/sliding in from the right depending on what item you clicked. I've seen lots of options for vertical accordions. Not as many, but a few, options for horizontal accordions. My situation was unique in that the click was not near or part of the scrolling panes. And it should disappear completely out of view if not the current pane so that if clicked again, it would again animate in. But if already in view – I wanted it to stay until the new pane coming in covered it completely.

Went through a few versions of this, and finally I have something I'm satisfied with. Here's the demo.

View source on that page to see the full html and jquery code. Now I'll walk you through what my logic was…

var lastpane;
var href;
var zcount = 2;
var inprogress = false;

That's just calling out some variables we'll be using in various iterations. "href" will be used to hold a reference to the current element/pane. "zcount" will make sure the current pane always has the highest z-index. "inprogress" will keep track of whether there is currently a pane being animated or not and will hold the other clicks/animations until the current pane animation is complete

$('a').click(function(){

Obviously, if this gets used in something other than a proof of concept, you'll probably have to be more specific about WHICH 'a' – ie – give the links you want to use for this a style like class="paneslider" and then change that to $('a.paneslider')… etc. In any case, the above just says, run the following whenever a link is clicked.

if (!inprogress && $(this).attr('title') != lastpane) {

Here I'm checking to make sure that an animation isn't already in progress, and the currently clicked link does not match the pane currently in view. (Which would have been defined as "lastpane"

inprogress = true;
zcount = zcount+1;

Set in progress to true right away so that we don't have conflicting animations. And add 1 to zcount (z-index), which will make the current pane appear above any other that is visible.

$(lastpane).css("z-index","1");

Give the last element pane shown (defined as "lastpane") a z-index of 1 to make sure it appears below the pane we want to have move across the screen.

var href = $(this).attr('href');

Now we'll redefine href as the current pane we want to animate. (This, by the way is set in the html – in the "a" tag using the "title" attribute).

$(href).css("left","800px");
$(href).css("z-index",zcount);

Make sure that the current pane is starting out completely out of view, and then giving it the highest z-index.

$(href).animate({"left": "0"}, 2000, function() {

Now we start our animation. Move it across to absolute position of 0 in 2000 milliseconds. As well we use a callback function to do the following after the pane finished moving.

$(lastpane).css("left","800px");
lastpane = href;
inprogress = false;

We move the pane that is now hidden under the current pane back to the "starting" position – and then we redefine "lastpane" with the pane that is now currently visible. And now that everything else is done, "inprogress" gets set back to false so we're open to run another pane across.

Then the rest of the code is all closing brackets etc.

Just for fun, I have another version of the sliding pane thing using the "easing plugin". Here's the demo using a bounce as it comes in.

7 Responses to "jQuery Side Scroll"

1 | rick

February 1st, 2009 at 2:56 am

Avatar

Have you seen these plugins? Similar to what you're doing… they seem to simply discard clicks on other links while processing a click.

http://demos.flesler.com/jquery/serialScroll/

http://demos.flesler.com/jquery/localScroll/

2 | Jennifer

February 1st, 2009 at 8:07 am

Avatar

Ah cool. Yeah probably can grab something from that. Thanks! I'll update the post when I get it working…

3 | Jennifer

February 1st, 2009 at 8:56 am

Avatar

Thanks Rick. Updated the script. Will update the post with instructions/explanation when I get back. (Running out the door for a few hours) :) Thanks for your help.

4 | rick

February 1st, 2009 at 11:42 pm

Avatar

Happy to help. I was looking around at similar stuff awhile ago and bookmarked those. Also found this site http://www.learningjquery.com/2007/02/more-showing-more-hiding which seems useful.

5 | kenny hall

March 3rd, 2009 at 4:26 pm

Avatar

hello, quick question.

Do you know if this sort of thing is possible with a wordpress blog? i've been trying to find something that would make it so, instead of scrolling up and down, it would store all my posts from left to right. far left being newest..does this make sense? is it possible? thank you!!

6 | Graham

April 8th, 2009 at 12:03 pm

Avatar

Nicely done. Thorough and intuitive. Can't wait to play with this script a bit. I actually have an idea for an implementation that could be REALLY sexy.

7 | Nick

May 30th, 2009 at 5:40 pm

Avatar

I'm wondering the same thing as Kenny. I'm wanting to build a blog and instead of scrolling down you navigate left and right on a WP blog.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements