Web development , php , ajax , symfony, framework, zend
In: web resources
18 Mar 2010
I’ve been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you:
var numChicks = $$('.slideshow').length; //simple!
The following code snippet will show you how to create a simple slideshow with MooTools; the script will also preload the images and feature a progress message. Why preload images? They make the slideshow more elegant and you can avoid an onLoad mess. Oh, and chicks…loads and loads of chicks.
<div id="slideshow-holder"> <div id="progress"></div> </div>
Basically just two DIVs which will hold content.
#slideshow-holder { width:600px; height:400px; background:url(spinner.gif) center center no-repeat #fff; position:relative; }
#progress { position:absolute; width:100%; text-align:center; color:#999; top:225px; }
The image holder is given set dimensions, starts with a background spinner, and its position set to relative; the images will all be positioned absolutely. The progress holder is set right below the spinner.
window.addEvent('domready',function() {
/* preloading */
var imagesDir = 'epics/';
var images = ['2.jpg','3.jpg','1.jpg','4.jpg','5.jpg'];
var holder = $('slideshow-holder');
images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
var progressTemplate = 'Loading image {x} of ' + images.length;
var updateProgress = function(num) {
progress.set('text',progressTemplate.replace('{x}',num));
};
var progress = $('progress');
updateProgress('text','0');
var loader = new Asset.images(images, {
onProgress: function(c,index) {
updateProgress('text',index + 1);
},
onComplete: function() {
var slides = [];
/* put images into page */
images.each(function(im) {
slides.push(new Element('img',{
src:im,
width: 600,
height: 400,
styles: {
opacity:0,
top:0,
left:0,
position:'absolute',
'z-index': 10
}
}).inject(holder));
holder.setStyle('background','url(logo.png) center 80px no-repeat');
});
var showInterval = 5000;
var index = 0;
progress.set('text','Images loaded. MooTools FTW.');
(function() {slides[index].tween('opacity',1); }).delay(1000);
var start = function() {
(function() {
holder.setStyle('background','');
slides[index].fade(0);
++index;
index = (slides[index] ? index : 0);
slides[index].fade(1);
}).periodical(showInterval);
};
/* start the show */
start();
}
});
});
The first set of variable declarations represent basic settings for the preloader: images, preload-message-updating, etc. We pass our Asset.images instance an array of images. When each image loads, we update the status message. When every image has loaded, we remove the status message and start the slideshow. That’s it!
Of course the above could be turned into the class….I’m just slightly lazy…Feel free to turn it into a class and share with everyone!
Don’t forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around!Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!
Create a Quick MooTools Slideshow with Preloading Images
Related posts:
This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.
4 Responses to Create a Quick MooTools Slideshow with Preloading Images
Wally Z
April 3rd, 2010 at 1:22 pm
It should be easier to do it as a Word macro.
barenakedchicken
April 10th, 2010 at 3:24 am
the inner divs will always stretch the outer divs. but this can also be done if you css styles
for the outer div set the following style attributes:
height to a fixed value
width to a fixed value
overflow=auto
now u can set the value of the inner divs . if the innerdiv is larger a scrollbar will appear in the outer div for navigating the inner divs.
Simon R
May 3rd, 2010 at 8:58 am
Well it's not as bad as in the old dial up days, but unless you pre-load your images then you can't. The most common use for that is/was for rollover graphics so you didn't have to wait for the rollover image to download.
This may help you do what you're after:
Of course pre-loading your images will make your page load a little slower initially, so you've got a trade off either way.
Hope that helps!
screwdriver
May 30th, 2010 at 8:59 am
Do you want to know what software to use or how to use a specific software? Sizing a .tga image is like most others.