Basics first #
Let’s start with the Markup and CSS. I used the same markup which I used on blog.kiskolabs.com as the foundation for this plugin, but I wanted to make it a bit better this time. The problem with my previous version was that if there was any content below the slideshow, you had to change the height between Media Query triggerpoints for it to work properly. Now we don’t have to specify/calculate height anymore, so this scales really smoothly.
<img src="1.jpg" alt="" />
<img src="2.jpg" alt="" />
<img src="3.jpg" alt="" />
The markup is really simple: you only need one container and few images inside that. Max-width on the container isn't necessary, but we might want to prevent images scaling too much.
img_visible class in CSS fixes the height problem I mentioned earlier. It’s basically applied to a slide when it fades in and removed when it fades out. The idea for using
position: relative on visible slide came from a comment on the previous blog post.
float: left makes the fix also work on IE6.
Making it a plugin #
There’s a pretty good tutorial on docs.jquery.com about writing your own plugins. It’s a good place to start if you are interested to learn how it works. After you become familiar with the concept you can try something simple first and build up from there.
I had three things in my mind when I started doing this. First, it should scale smoothly without any glitches, second, the whole plugin should be very light in file size and third, I wanted to avoid feature bloat. Because of the second and the third thing there isn’t very many options you can customize and I didn’t even plan to include much more in the future.
Hooking it up: #
auto: true, // Boolean: Animate automatically, true/false
speed: 1000, // Integer: Speed of the transition, in ms
timeout: 4000, // Integer: Time between transitions, in ms
pager: false, // Boolean: Show pager, true or false
nav: false, // Boolean: Show navigation, true or false
prevText: "Previous", // String: Text for the "previous" button
nextText: "Next", // String: Text for the "next" button
maxwidth: "none", // Integer: Max-width of the slideshow, in px
controls: "", // Selector: Where controls should be appended
namespace: "rslides" // String: change the default namespace used
Was it worth it? #
I think it was. I learned a ton about jQuery plugins + now I have something to use with new projects. I do know though, that there’s still a lot of things that I could probably do better, but given the time that I used for the development, I’m satisfied with the results. See the plugin in action on it’s homepage and check out the repository on GitHub. ❦
Update: Decided to change the name “AdaptiveSlides.js” to “ResponsiveSlides.js”. That just makes more sense here.
Update 2: I changed the markup to use unordered lists. Check out the new version.