Feed on
Posts
Comments

jQuery Panel Gallery Plugin

The other day I went looking for a simple way using jQuery to fade between a set
of images on a page. I found a few good plugins, some with many options, but none that
did exactly what I was looking for. InnerFade came close, as did jQuery Cycle, but neither had the effect that I wanted, nor the simplicity and reusability that I needed. So, being a coder, I set out to create my own plugin. I was mainly looking for a simple way to take a group of images and transition from one to the other in a smooth, visually appealing way. I’ve seen countless galleries where one image fades into another and I like that effect, but wanted to come up with something new, so I began writing my first jQuery plugin. I didn’t set out to make a “gallery” per se, however when I was done, I realized that that was really what I had ended up with. Let me take a moment to say that I’m fairly new to jQuery and even newer to writing jQuery plugins — in fact this my first one, however I’ve tested this as much as I can, and made it as small as I can (without compressing it), and it works with everything I’ve thrown at it. So to make a long story short, here’s what I ended up with:

So what’s the big deal? Well first off, and the best part in my opinion, is that not one image needs to be sliced or edited to work with this plugin, in fact the plugin handles everything itself. Next, you can choose the direction in which the transition occurs from left to right, right to left, top to bottom, or bottom to top. You can set the initial delay before the transitions begin, set the transitions to loop or stop after the last image, set the time it takes for each panel to appear, and set the delay between the images. Phew. All in a plugin that’s less than 5k uncompressed. Not bad.

Note: the latest version of the Panel Gallery is available here.

Download

Download version 1.1 here

Download jquery.panelgallery.js (version 1.0)

What’s So Great About this Plugin?

  • No slicing or editing of the images is needed
  • It’s just 5K
  • Easily configurable
  • Reusable on multiple containers

How to Use

First, all of your images should be the same size and wrapped in a containing element
(I recommend a div) which must have an ID. Any images will work, however transparent images aren’t recommended. Finally, your container element should be positioned (e.g. relative, absolute, etc.). The container needs to be positioned otherwise the images within it won’t end up in the right spot on your page.

<div id="container" style="position:relative">
	<img src="image1.jpg" alt="image 1" width="500" height="250" />
	<img src="image2.jpg" alt="image 2" width="500" height="250" />
	<img src="image3.jpg" alt="image 3" width="500" height="250" />
	.
	.
	.
</div>

All images *MUST* have the width and height declared otherwise the plugin won’t
work in Safari, Chrome, and any other webkit-based browsers. Also, all images should
be the same size. You can mix various image formats like Gif, Jpg, and Png, however
transparent images aren’t recommended.

To use the plugin you’ll need to have a copy of jQuery, or point to jquery on Google, and the plugin. Place the files on your site and link to them:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.panelgallery.js"></script>
<script type="text/javascript">
$(function(){
	$('#container').panelGallery();
});
</script>

That’s it! You can apply the panel gallery to any number of elements on a page.

Options

The following options are configurable:

  • sections – the number of panels to divine each image into (default: 3)
  • imageTransitionDelay – the time (in milliseconds) to pause between a
    completed image and the next image (default: 3000)
  • startDelay – the time (in milliseconds) to pause before beginning the first transition (default: 2000)
  • sectionTransitionDelay – the time (in milliseconds) to pause between each section of an image (default: 700)
  • repeat – whether to endlessly repeat the series of images (default: true)
  • direction – the direction of the transitions: “lr” (left to right), “rl” (right to left), “tb” (top to bottom), and “bt” (bottom to top) (default: “lr”)

Options are added when calling the script:

$('#container').panelGallery({
	sections:5,
	imageTransitionDelay :3000,
	startDelay:5000,
	sectionTransitionDelay : 700,
	repeat:false,
	direction:"tb"
});

Enjoy!

Demos

This is so gay true

Someone read my mind about twitter…

I started using Digg in 2005 and with each passing month I use it less and less. I hardly digg any stories anymore, and I usually visit once a twice a week to see what’s been driven up the popularity ladder. I’ve pretty much stopped submitting stories since they don’t seem to get any attention but in the process I discovered a weakness in the digg process that they should fix.

I call it digg ‘n ditch. It works like this. You find a story you want to submit and begin the process of adding it to digg. As soon as you submit the story (video, image, etc.) digg comes back and says sorry, someone beat you to the punch, and shows you that some smartass submitted the same link 20 minutes earlier. Then it sits there. On your monitor. Mocking you. Should I digg it you think? I was going to submit anyway so why not digg it? Oh screw it, it wasn’t that good anyway. And then you give up and browse the rest of the site or go back to filling out your TPS report.

And therein lies the flaw. A story you were going to submit and simultaneously digg, goes undugg. When digg’s system brings up their ‘oh hey sorry bud, better luck next time’ page, it should also automatically add your digg to it. You were ready to add your support to the exact same link anyway, so why not? You can always go through the exercise of undigging it if you’re really in a shitty mood and feeling petty, but you’re as lazy as I am and we know you’ll just leave it. The way it should be. Dugg.

I’ve been coding in PHP for probably eight years and thought I knew the language pretty well. Was I surprised the other day when I came across someone else’s code and stared at it blankly, wondering why they had done what they did, and why wasn’t it generating an error. Specifically they had something like this:

echo "Your IP address is {$_SERVER['REMOTE_ADDR']}";

Why were those curly brackets in the echo statement and why wasn’t this giving me errors? I removed the curly brackets and as expected, I received syntax errors. Apparently the {} were magical. Now in my own code, I would typically write the above line like:

echo "Your IP address is " . $_SERVER['REMOTE_ADDR'];

or

echo "Your IP address is $_SERVER[REMOTE_ADDR]";

Now method one is perfectly valid code, but it’s a bit ugly having to starty and stop the string to insert a variable. Method 2 is somewhat wrong as the quotes have been removed from the server array key.  According to php.net, “Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not.” But with this new, more compact method, I could make my code cleaner.  I was off to learn more.

After some searching I discovered that PHP will allow you to place curly brackets around a variable in a string (either around the entire variable or with the dollar sign sticking out). “If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. Similarly, an array index or an object property can be parsed. With array indices, the closing square bracket (]) marks the end of the index. The same rules apply to object properties as to simple variables. ” Who knew? I must’ve missed this day during PHP 101 class. Here’s the example from the manual:

<?php
$beer 'Heineken';
echo "$beer's taste is great"// works; "'" is an invalid character for variable names
echo "He drank some $beers";   // won't work; 's' is a valid character for variable names but the variable is "$beer"
echo "He drank some ${beer}s"// works
echo "He drank some {$beer}s"// works
?>

So in a nushell, when using variables in a string you are allowed to enclose the varable in curly brackets to allow variable expansion without having to jump in and out of quotes, or drop quotes. Who doesn’t love PHP?