Web Development Blog

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

11.01.2009 10:19PM by Evan Johnson

(Plug: Looking for an easy, fast, affordable way to create custom Facebook landing tabs? Check out my new product: http://splashtab.com Thanks!)

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!

Gravatar Image
On Mar 19, 12:35 PM, (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, 04: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, 08: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, 09: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, 10:28 PM, (Link)
Jerrod said:

you just saved my sanity.

Great article!

Gravatar Image
On Jun 9, 10: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, 04: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, 10: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.

Gravatar Image
On Aug 5, 03:33 AM, (Link)
CliveO said:

I’ve tried everything with this, copied the code exactly and still can’t get it to slide…it’s got me pulling my hair out! ;)

Gravatar Image
On Aug 15, 08:17 AM, (Link)
Juliana said:

Same as CliveO,
It works in Firefox, but it wont work in IE,

is there a workaround for it not working in IE?

Gravatar Image
On Aug 17, 10:42 PM, (Link)
Evan Johnson said:

@CliveO and Juliana – Sorry folks, no time to help you debug this old piece of code at the moment. It worked back when I wrote it, I’m not sure what could have changed since then. I am not aware of any IE bugs either. But if anyone else can help, feel free to leave a comment!

Gravatar Image
On Sep 2, 10:24 AM, (Link)
Dave said:

Hi Evan,

This has been great.

I just have a quick question. Using this code, would it be possible to create an automatic slideshow that loops continuously?

cheers

Gravatar Image
On Sep 2, 01:07 PM, (Link)
Evan Johnson said:

Unfortunately no. Last I checked (and this may change as Facebook transitions to iFrame tabs) with an FBML Tab you cannot have "auto start" JavaScript. None of the "onload"/"document.ready" stuff works. It’s a security feature or something. You CAN make one that loops, but the user will have to click on it to start it.

Gravatar Image
On Sep 3, 02:49 PM, (Link)
andres said:

Hey I figured it out and would be happy to help anyone with questions. I spent two days working this out. Email me through the contact form on my site, Andresfgarcia.com.

Gravatar Image
On Oct 28, 11:15 AM, (Link)
Danielle said:

I am manipulating the code so that it shows one image at a time and scrolls through 4 altogether. I’ve changed the JS variables to:

var numslides = 4;
var slidesvisible = 1;
var currentslide = 0;
var slidewidth = 47;

and it’s still showing up 4 images, not just one…

can someone tell me what I need to do?

I’ve also changed the code on the markup so there’s only 4 images. Everything else is working perfectly!

Thank you!!!

Gravatar Image
On Nov 2, 08:34 AM, (Link)
levani said:

This script doesn’t work in IE8. Can anyone please help?

Gravatar Image
On Nov 3, 03:07 AM, (Link)
irtiza104 said:

Hi,

Thanks for this article. But can you please tell me if its possible to have a vertical carousel instead of a horizontal one? How can i do it?

Thank you.

Gravatar Image
On Nov 3, 01:28 PM, (Link)
javier said:

hi,

very interesting document, i have a great doubt, is it possible use jquery in a facebook aplicaction iframe used in tabs ???

example:
http://www.facebook.com/pages/Murcia/Ancor-Consultores/126669834014339?v=app_270776673375

i don’t get it works !!!!

Gravatar Image
On Nov 4, 09:43 AM, (Link)
Evan Johnson said:

@irtiza104 – I’m sure you could make it vertical with a little work!

@javier – As far as I know, until Facebook officially moves to iFrame tabs, it’s not possible to use jQuery in a tab. You can, I think, in a regular Canvas iFrame app, but not on a tab. There is a way with a little "hack" to make an iFrame tab though, which I commented about here: http://www.chilipepperdesign.com/2009/10/11/how-to-make-custom-facebook-fan-page-tabs-with-fbml-part-2#c000260

YMMV though…

Gravatar Image
On Nov 9, 08:13 AM, (Link)
devin said:

This was very helpful to me. Thank you! I made a simple modification to support multiple slideshows. I simply made a minor change to the html/css and used a json object array to store options and element ids

var ss1 = {"options": [ {"numslides": 3, "slidesvisible": 1, "currentslide": 0, "slidewidth": 147} ], "id": [ {"left_button": "ss1_left", "right_button": "ss1_right", "slideshow_inner": "ss1_inner"} ]
};

I couldn’t post the rest of the code because of a line break and character replacement issues when submitting..

Gravatar Image
On Dec 2, 02:14 PM, (Link)
Olivia said:

Hey,
is there a possibility that so that it works dynamicaly? Something like a timer or nice things like that?

Gravatar Image
On Dec 2, 03:58 PM, (Link)
Evan Johnson said:

@Olivia – Assuming you mean "starts automatically", no. You could probably set something up so the user can click "Start" and then the carousel advances automatically based on a timer, but Facebook disables all "auto start" onLoad JavaScripts so it will require user interaction (like a click) to start it.

Gravatar Image
On Dec 20, 09:19 AM, (Link)
Monty said:

For all the IE issues, this is the solution that worked for me…
Include all the styles in your fbml from an external stylesheet…works like a charm…use the <link> tags…enjoi..

Gravatar Image
On Jan 4, 06:23 PM, (Link)
Amanda said:

@Monty – THANK YOU!!! This worked for me as well.. all of my IE issues are solved.

Gravatar Image
On Jan 19, 10:48 PM, (Link)
denise said:

@Evan Thank you so much for posting this!

@Monty or @Evan I am a bit new to fbml, can you give an example or explain exactly how to use the <link> tags.

is it something like the following:

<link="http://myurl.com/file.js">
<link="http://myurl.com/file.css">

<div id="slideshow_wrapper"> <!-- slideshow markup goes here --> </div>

Gravatar Image
On Jan 20, 09:21 AM, (Link)
Evan Johnson said:

@denise – Looking at the Facebook FBJS documentation, I believe to link in the script you need to something like this:

<script src="http://myurl.com/file.js"></script>

Read more about it here, under the "Include files" section towards the bottom: Facebook FBJS Docs

  Subscribe to article comments