My First JQuery Plug-In: A Slideshow!!

JQuery Plug-In: Simple Slideshow v2.0

I know, I know. There's about a gazillion slideshow plug-ins for JQuery. Trust me, I've seen em, I've used them and I've been impressed by them. But at times, the websites I'm working on, call for a simple little slideshow. That's all, nothing fancy. Just somethings that dissolves images, maybe some basic captioning and perhaps a little option to center the images or not.

A few years back, I made a little script that does just this. It was simple, had a small footprint and it did its job fairly well. The drawback was that it only allowed me to place one slideshow on a page at a time. So today I decided to re-write it and I figured while I was at it, convert it into a JQuery plug-in as well.

So let me introduce my Simple JQuery Slideshow. It has a few features you can control as well as the ability to pause a slide when a user mouseover's the cotainer. To use, you can download one (or both) of the following scripts:

CJ Simple Slideshow v2.0 JQuery Plugin
CJ Simple Slideshow Minified v2.0 JQuery Plugin

Requirements

I have only tested this with JQuery version 1.3.2. Not sure if it works with older versions, If you give it a try, come back and let me know!

Implementing

Implementing the plug-in is pretty straight foward, you just need to have a container that contains a group of images, like so:

<div id="MySlideShow" style="display:none">
   <img src="sample1.jpg" width="250" height="150" alt="This is caption 1." /><br />
   <img src="sample2.jpg" width="250" height="150" alt="This is caption 2" />
</div>

So now that we have declared our slideshow container, all we need to do is call the plug-in to activate it. Oh yeah, you might notice that I have added a <STYLE> declaration and also I have a <BR> in there. The <STYLE> is used to set the CSS display property to none. This ensures that all the images are hidden while the page loads. And the line-break is so the images display somewhat nicely for people that have JavaScript turned of or older browsers. Neither of these items are required, but it makes for a better experience for your visitors. Anyways, back to starting our slideshow.

The most basic implementation is as follows: (This code needs to be placed in the header section of your page)

$(function() {
   $(document).ready(function() {
      $("#MySlideShow").cjSimpleSlideShow();
   });
});

I tried to make the plug-in work without you having to pass an arguments to it. It will attempt to determine the image container's width and height settings, but this method is far from full-proof. The best way to activate the plug-in is to pass these values or declare a CSS rule for the container:

Passing width and height values example

$(function() {
   $(document).ready(function() {
      $("#MySlideShow").cjSimpleSlideShow({
         width: "250px",
         height: "150px"
      });
   });
});

Declaring a CSS rule example

#MySlideShow { display: none; width: 250px; height: 150px; }

User Settings

You have a few options other than the width and height, that you can pass to the plug-in. These change basic functionality and are listed below:

Argument Description Default
autoRun Determines if the slideshow starts automatically. If you set this to false, then you will need to start the slideshow by calling $("#MySlideShow").init(); true
bgColor The background color to use. #ffffff
centerImg This is still in the beta stage, but it will attempt to center the images within the main container box. false
delay The delay in miliseconds between slide transitions, 3000
dissolve The amount of time in miliseconds that it takes to do the transition between slides. 70
height The container height. This accepts standard CSS height values. 0
showCaptions Determines wether or not to display the slide captions. If set to true, the plug-in will use the value of the images ALT tag for the slide caption. true
textColor Determines the color to use for text within the container. Accepts standard CSS color values. #ffffff
textFont Determines the font family to use for text within the container. Accepts standard CSS font-family values. Arial, Helvetica, sans-serif
textShadow Determines if to display a text shadow for text used within the container. Only visible for browsers which accepts this option. true
textSize Determines the font size to use for text within the container. Accepts standard CSS font-size values. 11px
textStyle Determines the font style to use for text within the container. Accepts standard CSS font-style values. normal
textWeight Determines the font weight to use for text within the container. Accepts standard CSS font-weight values. bold
width The container width. This accepts standard CSS width values. 0

Example & Conclusion

Need to see it in action? No problemo, you can see a working demo here. Hope this works for you and if you have any questions or problems, be sure to let me know.

Share: del.icio.us digg StumbleUpon Facebook Technorati Fav Mixx Spurl newsvine Furl reddit FARK Yahoo! My Web Wists Simpy BlinkList BlogMarks smarking Ma.gnolia MSN Live Google Bookmarks Segnalo diigo excites Netvouz

5 responses to "My First JQuery Plug-In: A Slideshow!!"

glig
glig says:
Nice plugin.

One note about the documentation. You use the word "excepts" where you want to say "accepts". Normally I wouldn't point out something like this but you use it several times and it has almost the reverse meaning of what you intend. Could be especially confusing to people whose first language is not English.

Thanks
Doug
Doug responds:
@glig - haha... sorry about that. It was pretty late and I used copy & paste on most of the table stuff. I wish more people would call me out on my horrible spelling! It has since been fixed. Thanks.
Marco
Marco says:
hey,

I just tested your script but it wont't work in IE7. did you already tested it?

it simply doesn't display any of the images..
Doug
Doug responds:
@Marco - I haven't tested it yet, I'm going to right now. My guess is that it's a CSS issue. Did you pass any parameters?
Marco
Marco says:
hi Doug,

I also think the problem relies on a css issue. I got it working by setting display to block manually for MySlideShow before initializing the script.

hope that helps you to get the problem.

greetz,
marco
Doug
Doug responds:
@Marco - Well, there's definately something screwy going on with it. I think it has to do with trying to determine the elements attributes without any being set. I played around with it a little, but it's going to take me a little more time than I have right now. I will take a look at it a little more tonight and see if I can't track what the problem is exactly. I may have to strip some of the code and force you to supply the image dimensions for the <div>. We'll see.

Thanks!
Doug
Doug says:
Ok, I've made a version 2.1. I've updated the demo on this page with the new code, but I'm going to make a new blog entry for the changes, since I had to rewrite a lot of the code and strip out some functionality.
Nick
Nick says:
Hi

Is there any solution to the IE7 problem yet?

Thanks

Nick.
Doug
Doug responds:
@Nick - Yep, I re-worked it today. Check out the newer blog entry: http://www.cjboco.com/post.cfm/my-first-jquery-plug-in-my-first-failure-a-slideshow-redux

Don't Be Shy, Leave A Reply!




You are currently posting as an unregistered user.
This means that your comment will be reviewed prior to going live. If you are a registered user, please . New user? No problem, register for an account, it's FREE! Benefits include, posting instantly, screen name protection, collaboration recognition, subscribe to article updates, and so much more!