Web Development Blog

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

11.01.2009 09: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, 03: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, 06: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, 01: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, 08: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!

Gravatar Image
On Mar 19, 11:35 AM, (Link)
Matt said:

The slide show isn’t sliding for me. I’ve reset paths, and the values (like # of slides, etc) for my little test. The arrows and three slides display. But clicking on the right arrow doesn’t work.

I didn’t touch the js beyond "var numslides = 7;" which I set to 5.

Thanks
Matt

Gravatar Image
On Mar 21, 03:24 PM, (Link)
Evan Johnson said:

@Matt – it’s been a while since I looked at this, but I would check the FBML markup to make sure the right arrow has the following attribute: onclick="goright(); return false;". Sounds like it’s not calling the goright() JS function. Good luck!

Gravatar Image
On May 12, 07:26 AM, (Link)
Dan said:

If you click quickly on the right or left arrow [faster than the image transitions] the count increases but the pictures don’t move fast enough, anyway to disable the onclick until the transition is finished?

Gravatar Image
On May 13, 08:18 AM, (Link)
Tony Maserati said:

Hi!

Unfortunately my markup is a little bit different and I also need the thing to slide 3 items at once (so the user doesn’t have to click so much), preferably with some easing. I am really not good with this but here is what I’ve come up with so far – http://pastebin.com/eDhhEUem – any feedback would be greatly appreciated! Thanks for the article!

Gravatar Image
On May 28, 09:28 PM, (Link)
Jerrod said:

you just saved my sanity.

Great article!

Gravatar Image
On Jun 9, 09:02 AM, (Link)
Evan Johnson said:

@everybody – Sorry it’s taken me a while to respond (and my response will be a little disappointing anyway.)

I’m afraid I have not revisited this since I wrote it, and don’t have to time to work out bugs in my code now. The code for the actual slider was a quick example I did not put a whole lot of time into. For perfecting your own version, there are some good examples on the (one) page of documentation Facebook under the Examples section, including Easing:
http://wiki.developers.facebook.com/index.php/FBJS/Animation

And as for the Animation queue problem @Dan mentioned, I wonder if use can use the "stop()" trick like in jQuery? See if this works with the Animate library as well:
http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup

Gravatar Image
On Jul 26, 03:16 AM, (Link)
shafraz said:

hi,
im new to this fmbl ….. so can anyone please tell me where to put this css and javascript code. i tried pasting all javascript, css and the markup in FBML code area but it dnt work…
thanks in advance………

Gravatar Image
On Jul 27, 09:24 AM, (Link)
Evan Johnson said:

@shafraz – Where is the "FBML Code Area"? Do you mean you are using the Static FBML application? It should work OK there, just remember that when including JS and CSS in the markup "code area" you need to wrap them in the <script> and <style> tags (respectively). I don’t have those tags in the code I noticed, so that might be confusing.

  Subscribe to article comments