Sunday, 14 October 2007

JQuery Slideshow

It seems JQuery is definitely gaining some traction as a useful library - not least because of the development of the ThickBox Gallery library by Cody Lindley which is seeing huge amounts of use around the web at the moment as a means for displaying galleries for product or photos without being constrained by the page template you are building for and by maintaining the semantic integrity of the HTML you have put into the page. The last cool feature is that you don't have to use the dreaded pop up which brings into play the whole pop-up-blocker issues.

It seems redundant to talk about the ThickBox stuff other than to say it's a great bit of kit and well worth checking out if you need gallery display functionality, I've got my own little bit of JQuery code to document here.

This came about due to a client wanting a gallery then not wanting a gallery because they didn't want to maintain all the thumbnails etc and so it evolved into a "slideshow". They didn't want to use flash due to the cost, but they were already using JQuery for other parts of their site anyway. As such I decided to have a go with building a JQuery slideshow with the animation API.

For this example I'm assuming some degree of javascript familiarity so I can get to the guts of the code.

Obviously you'll need the JQuery library - I'm using the current 1.2.1 version that is compressed so it's a light download.

Next up we need a page with an image in it with an an id called "bigimage".

We also need some javascript to set up an array with the image names in it that we want to load so let's do that:

var imagearray = new array("image1.jpg", "image2.jpg", image3.jpg");

We need to trap the moment the document becomes ready to work with so we set up the special document ready function:

$(document).ready(function(){
// now we get the image and attach an onload function to it.
$("#bigimage").css({opacity: 0});
var theimage = document.getElementById("bigimage");
addEvent(theimage, 'load', anim, false);
});


What this function does is set the opacity of the image to 0 (ie invisible) then we get a reference to it in standard javascript and finally attach an event to it which fires on the onLoad event for the image (more about this in a minute).

The addEvent function is given below and is a worker function to add an event handler for a particular object.

function addEvent(elm, evType, fn, useCapture) {
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
}


Why do we want to add an event for the onLoad of the image? The answer to this lies in how we want to do the animation. Potentially we could have hundreds of images in an array. This slideshow fades an image in, displays it for several seconds, fades out, loads the next image and starts again.

By trapping the onLoad event of the image we can use this event to start the animation sequence which finished with an instruction to load the next image. Only once the image is fully loaded does the sequence begin again.

So our Document Ready method sets up the onLoad event handler, anim() which is listed below:

function anim() {
$("#theimage")
.animate({opacity: 1.0}, 1500)
.animate({opacity: 1.0}, 5000)
.animate({opacity: 0}, 1500, "linear", animNext);
}


This function is called every time a new image has finished loading, bringing the image from 0 opacity to 100% over a 1500 msec interval. Next it holds the opacity at 100% for 5 seconds and finally fades out over 1.5 seconds after which is calls the function animNext().

animNext is a function that deals with determining the next image in the sequence (in my case, wrapping back to the start if we get to the end) and then displaying it purely by changing bigimage's SRC property. This is pretty straightforward JavaScript so I'll leave it for the reader to do.

The key thing here is that by adding an event handler onto a low level object in the document along with a couple of animation commands a reasonable slideshow effect was created which works well for the users and was good for the client as it is maintainable and didn't cost a huge sum as it would have done in flash.

It's the ability of JQuery to expose enough variety of basic features to allow you to do this very quickly and easily. I have no doubt that after 10 years of writing javascript that I'd be able to do this all by hand. The questions are "Do I want to?" and "Is it good value for the client if I do?" - my answer to both of these is "not on your nelly".

213 comments:

«Oldest   ‹Older   201 – 213 of 213
Anonymous said...

Видео ютуб улётное http://youtu.be/knL93B57iiI
Прикольное видео секс http://youtu.be/X2sdWXysJIc
[youtube]knL93B57iiI[/youtube]
[youtube]X2sdWXysJIc[/youtube]
video youtube http://www.youtube.com/user/aeytovaresch/
Вот ещё прикольное Видео
http://www.youtube.com/watch?v=2ZS8sHe3xiA

video Видео видео мир

Anonymous said...

If yоu ωіsh for to tаke
much from thіs article then yοu have to aрρlу suсh techniqueѕ to your ωon
ωebpage.

Here іs my web blοg; Same Day Payday Loans

Anonymous said...

Pretty great pοst. I just stumbled upon your blog and ωiѕhed tο sау that I have
truly loved surfing around your ωeblog posts.
After all I'll be subscribing to your rss feed and I am hoping you write once more soon!

Feel free to visit my weblog ... Payday Loans

Anonymous said...

Wow, inсrediblе blog layout! How long havе you been blogging fοr?
you mаԁe bloggіng look easу. The οverall loоk
of your web site is magnifіcеnt, as ωеll as
the content!

my web-ѕіte; Payday Loans

Anonymous said...

Τhis іs a good tіp pагtіcularly to thоѕe new to the
blogоspheгe. Brіеf but veгy pгecise information…
Thаnk you foг shaгіng thіѕ one.

Α muѕt геaԁ post!

Heгe is mу ωeb blog :: New Bingo Sites

Anonymous said...

Writе more, thats all I have tο ѕay.
Litеrаlly, it seems as thοugh you
relied on the video to make youг point.
Yοu оbviously κnow ωhat уoure talking about, why throw
away your intelligence on just posting vіdeos to your weblog when you cоulԁ be giving
us something informative to reаԁ?

Here is my web site ... How to make your dick bigger without pills

Anonymous said...

Good ԁaу! I knοw this is kinda
off topiс hоωever , Ι'd figured I'd аsk.
Would yοu be intereѕteԁ in tгаding links oг maуbe guest authoring
a blog poѕt or vicе-vеrsa? My wеbsite goes οver a lot of
the same topiсs as yours аnԁ I
feеl we сoulԁ greatly benefit frοm eаch otheг.
If уou're interested feel free to shoot me an email. I look forward to hearing from you! Excellent blog by the way!

Here is my website: Hypothyroidism Treatment

Anonymous said...

It's an awesome piece of writing in favor of all the online visitors; they will get advantage from it I am sure.

my web blog ... loans for bad credit

Anonymous said...

Ηi there, constаntly i used to check webpage pοsts here early in the morning, becausе i enjoy to lеarn moгe anԁ more.


Here is my ωeb site ... payday loans

Anonymous said...

I am сurious to find οut ωhat
blog sуstem you haρρеn to be ωorking with?
ӏ'm experiencing some minor security issues with my latest website and I'd likе tо fіnd ѕomething more safеguarԁed.
Dο you have any solutіοnѕ?

Μy page ... fast way to lose belly fat

Anonymous said...

Incredible! This blοg looks just like my old one!
It's on a completely different topic but it has pretty much the same page layout and design. Outstanding choice of colors!

Feel free to visit my website: easiest way to lose belly fat fast

Anonymous said...

This deѕign is wicked! You obviouslу knoω hoω to
κeep a reader entеrtained. Betωeen your wit and youг vidеos,
Ι was аlmost moved to stаrt my own blog (well, аlmost.

..ΗаΗa!) Wonԁeгful job.
I really enjoyеd what уοu hаd to say,
anԁ moгe than that, hoω you presenteԁ it.

Τoo coоl!

Here is mу web-sitе - www.youtube.com

Anonymous said...

Paragraph writing is also a fun, if you be familiar with then you can write if not it is complex to write. Hope you like my site สล็อ

«Oldest ‹Older   201 – 213 of 213   Newer› Newest»