Web Design Blog

A simple FBJS image carousel (or slideshow) for Facebook page tabs

11.01.2009 10:19PM by Evan Johnson

This little code snippet is really basic, and not actually that good or clever. You can make a much nicer slideshow or carousel if you put a little time into it. But I just want to demonstrate how many of the nice JavaScript effects we are used to on the "Web 2.0" are also possible on a Facebook Page via Facebook’s Animate library. It’s not as robust as jQuery or Scriptaculous, but since you can’t import those libraries to a Tab the Animate library will have to do. :)

The final result looks like this:

And you can view a live demo on the CPD Facebook Page

For instructions on how to create your own custom Facebook tabs, read my tutorial here.

To begin, here is the simple FBJS <script> code:


var numslides = 7;
var slidesvisible = 3;
var currentslide = 0;
var slidewidth = 147;
function goright() {
	if (currentslide <= (numslides-slidesvisible-1)) {
		Animation(document.getElementById('slideshow_inner')).by('right', slidewidth+'px').by('left', '-'+slidewidth+'px').go();
		if (currentslide == (numslides-slidesvisible-1)) {
			Animation(document.getElementById('right_button')).to('opacity', '0.3').go();
			Animation(document.getElementById('left_button')).to('opacity', '1').go();
		} 
		if (currentslide < (numslides-slidesvisible-1)) { Animation(document.getElementById('left_button')).to('opacity', '1').go(); }
		currentslide++;
	}
}
function goleft() {
	if (currentslide > 0) {
		Animation(document.getElementById('slideshow_inner')).by('left', slidewidth+'px').by('right', '-'+slidewidth+'px').go();
		if (currentslide == 1) {
			Animation(document.getElementById('left_button')).to('opacity', '0.3').go();
			Animation(document.getElementById('right_button')).to('opacity', '1').go();
		}
		if (currentslide > 1) { Animation(document.getElementById('right_button')).to('opacity', '1').go(); }
		currentslide--;
	}
}

Here is the CSS:


	#slideshow_wrapper { width:530px; clear: both; margin-bottom: 20px; }
	#slideshow { overflow: hidden; width: 435px; float: left; position:relative; margin-right: 5px; }
	#slideshow_inner { position: relative; width: 1250px; }
	#slideshow_inner a { opacity:0.7; margin: 0 7px; }
	#slideshow_inner a:hover { opacity: 1; }

And finally, here is the markup:


<div id="slideshow_wrapper">
			<img id="left_button" src="http:/yoururl.com/images/left_button.gif" onclick="goleft(); return false;" style="float: left; display: block; width: 41px; cursor: pointer; opacity: 0.3;" />
		    <div id="slideshow">
		        <div id="slideshow_inner">
		            <a id="slide1" href="http://yoururl.com/link1" title="Oronaut Outdoor Blog"><img src="<?php echo $basepath ?>images/slide1.jpg" /></a> 
		            <a id="slide2" href="http://yoururl.com/link2" title="Colorado Ski Mountaineering Cup"><img src="<?php echo $basepath ?>images/slide2.jpg" /></a> 
		            <a id="slide3" href="http://yoururl.com/link3" title="PowderBlog"><img src="<?php echo $basepath ?>images/slide3.jpg" /></a> 
		            <a id="slide4" href="yoururl.com/link4" title="Highland Meadows HOA"><img src="http:/yoururl.com/images/slide4.jpg" /></a> 
		            <a id="slide5" href="http://yoururl.com/link5" title="United State Ski Mountaineering Association"><img src="http:/yoururl.com/images/slide5.jpg" /></a> 
		            <a id="slide6" href="http://yoururl.com/link6" title="Circle S Seeds"><img src="http:/yoururl.com/images/slide6.jpg" /></a> 
		            <a id="slide7" href="http://yoururl.com/link7" title="Rita Designs"><img src="http:/yoururl.com/images/slide7.jpg" /></a> 
		        </div>
		    </div>
		    <img id="right_button" src="http:/yoururl.com/images/right_button2.gif" onclick="goright(); return false;" style="float: left; display: block; width: 41px; cursor: pointer;" />
			<p>Click on an image to leave Facebook and visit the Portfolio.</p>
		</div>

(One thing to note when scripting FBJS for Tabs: onLoad does not work. All JS must be started with a trigger event of some sort, even if it is as simple as a mouseOver.)

This is just a starting point. My code is not very generalized, the opacity effects have IE issues (as usual), and there are probably other issues besides. But have fun building on this, and create some sexy animated Facebook tabs!

Comment

Gravatar Image
On Nov 23, 04:03 PM, (Link)
Steve said:

Really helpful tutorial.

Running into a problem, however. I am copying your code word-for-word and replacing the images, but the slider will not budge in my test-app.

Can you double check your code?

Gravatar Image
On Nov 29, 07:03 PM, (Link)
Evan Johnson said:

@Steve

I did find a problem in the FBJS script code. The "==" comparison sign is a Textile reserved "word" so my blog was interpreting it and hiding it! It is fixed now, I hope the code works again. Sorry, and thanks for letting me know about the problem.

Gravatar Image
On Feb 16, 02:33 PM, (Link)
AJ said:

Hi Evan,

Is there a way to stack the carousel? For instance, if you’d like to have two or three carousels in the same page stacked. Thanks!

Gravatar Image
On Feb 22, 09:33 PM, (Link)
Evan Johnson said:

@AJ – Yes, you can certainly stack them, but you’ll have to modify the code. It will not work as-is. The FBJS JavaScript will need to be generalized so you can pass in different IDs instead of just slideshow_inner as I am using here. I hard coded them. It shouldn’t be too hard to change though. Good luck!

  Subscribe to article comments