cooking, programming and everyday life xrado

Sunday, April 13, 2008

mooFader - image fader slideshow

i needed one for the website i was working on, so i made small class that works with mootools 1.11.
head:
<script type="text/javascript" src="mootools-1.11.js"></script>
<script type="text/javascript" src="moofader.js"></script>
<script>
var images = Array('<?=implode("','",glob('images/*.JPG'));?>');
window.addEvent('load', function(){ new mooFader('fader',images); } );
</script>
html:
<div style="position: relative; width: 390px; height: 390px;" id="fader">
	<img src="images/rado.JPG">
</div>
script:
var mooFader = new Class({

	options:{
		duration: 3000,
		fade: 1000
	},

	initialize: function(el,im,options) {
		this.setOptions(options);
		this.holder = $(el);
		if(!this.holder) return;
		this.starter = this.holder.getElement('img');
		this.starter.setStyle('position','absolute');
		this.im = im;
		this.faders = [];
		this.images = im.length;
		this.counter = 0;
		this.change.periodical(this.options.duration,this);
		new Asset.image(this.im[this.counter]);
	},

	change: function(){
		if(this.counter > this.images-1) this.counter = 0;
		var img = new Element('img',{
			'src'	: this.im[this.counter],
			'styles': { 'position':'absolute' }
		}).injectTop(this.holder);
		var fader = img.getNext();
		new Fx.Style(fader,'opacity',{ duration:this.options.fade, onComplete: function(el){ fader.remove(); } }).start('1', '0');
		this.counter++;
		new Asset.image(this.im[this.counter]);
	}
});

mooFader.implement(new Options, new Events);
Demo page can be found here.
I was looking for something as simple as this for a Joomla 1.5 (old Mootools) template, but I found there was a little problem that made the first image in the array last double the time if it was the same inside the holder (the fader waits X duration before changing to index 0, so if index 0 is the same image SRC inside the holder there won't be any visual change until next index is called after another X duration)

So, to fix this I just added a new integer option "start" which sets the initial counter value (this.counter = this.options.start), so in my case:
mooFader('slideshow', slides, {duration:5000, fade:1000, start:1});

I could also force a first change calling "this.change()" at right the end of the intialization function, and optionally to eliminate a almost imperceptible "ghosting" effect, set a ".first" class in the IMG tag inside holder DIV with visibility set to "hidden" or simply use a transparent gif instead of the first image inside the array.

The problem with these last options is that if javascript is OFF no image will be shown.

Hope it helps,

Thanks for the script!

thanks for sharing you opinion, greetings

http://www.uggbootsoutlet.com.co
http://www.ralphlaurenoutletonlinepolo.us.com
http://www.ralph-laurenoutlet.me.uk
http://www.uggsale.us.com
http://www.uggoutlet.net.co
http://www.adidasshoesoutlets.us.com
http://www.birkenstocks.us
http://www.nikeshoes.in.net
http://www.cheapjordans.com.co
http://www.moncleroutlets.us.org
http://www.yeezyboost.us.com
http://www.ugg-slippers.us.com
http://www.cheapraybansunglassesofficial.us.com
http://www.coachoutlets.in.net
http://www.ralphlaurenoutletonlines.us.com
http://www.cheapuggssale.com.co
http://www.nikeoutletonline.us.com
http://www.ralph-laurenoutlet.com.co
http://www.beatsbydrdre.org.uk
http://www.uggs-australia.it
http://www.coach-outletonline.us.org
http://www.hermes-handbags.us
http://www.nikeshoes.us.com
http://www.ralph-laurenuk.co.uk
http://www.adidas-yeezyshoes.us.com
http://www.adidasnmd.us.org
http://www.coachoutletsonlines.us.com
http://www.coachfactoryoutletsonlines.us.com
http://www.goosecanadaoutlet.us.com
http://www.nikehuaracheshoes.in.net
http://www.katespade.in.net
http://www.burberry-outlet-canada.ca
http://www.catboots.us
http://www.burberryoutletsale.com.co
http://www.canadagoose-outlet.name
http://www.pandoracharmsuk.uk
http://www.moncleroutlet.in.net
http://www.hermesbirkin.in.net
http://www.coach-factoryoutletonline.us.org
http://www.cheap-uggsboots.us.com
http://www.redbottomsshoes.us.com
http://www.pandora.us.com
http://www.coachhandbagsoutlet.us.com
http://www.supremeuk.org.uk
http://www.michael-korsoutlet.ca">http://www.michael-korsoutlet.ca
http://www.pandoracharmssaleclearance.me.uk
http://www.ralphlaurenoutlet.us.org
http://www.uggs.net.co
http://www.tomsshoes.us.com
http://www.pandoracharms.us.com
http://www.michaelkorsoutletonlineclearance.in.net">http://www.michaelkorsoutletonlineclearance.in.net
http://www.air-jordans.us.com
http://www.kate-spade.us.com
http://www.katespadeoutletonline.us.com
http://www.coachoutletus.us.com
http://www.cheapjordanshoes.us.com
http://www.jordanretro.name
http://www.uggsbootscanada.ca
http://www.nfljerseyscheapwholesale.us.com
http://www.louboutinshoes.in.net
http://www.coachoutlets.us.org
http://canadagoose.jacketsoutlet.us.com
http://www.tomsoutletstore.in.net
http://www.valentinoshoesoutlets.us.com
http://www.cheapuggs.in.net
http://www.christianlouboutinshoes.us.org
http://www.pandora-charms.us.org
http://www.mlbjerseys-cheap.us.com
http://www.pandora-uk.me.uk
http://www.longchamphandbags.me.uk
http://www.mbtshoes.us.org
http://www.red-bottoms.us.com
http://www.michaelkors-handbags.us.org">http://www.michaelkors-handbags.us.org
http://www.moncler-outlet.us.com
http://www.michaelkorsoutletclearance.in.net">http://www.michaelkorsoutletclearance.in.net
http://www.officialcoachoutlet.us.com
http://www.cheapnfl-jerseyswholesale.us.com
http://www.canadagooseoutlet.cc
http://www.coachoutletstoreonlineclearances.in.net
http://www.coachoutletstoreonline.com.co
http://www.nikeairmax.in.net
http://www.yeezyboost-350.co.uk
http://www.louisvuittonoutlet.us.org
http://www.katespadehandbag.us.com
http://www.coachoutlet.in.net
http://www.oakleysunglassses-cheap.us.com
http://www.uggs-outlet.net.co
http://www.michaelkorsoutlet.co.uk">http://www.michaelkorsoutlet.co.uk
http://www.canadagoose-outlet.net.co
http://www.adidasnmd-shoes.us
http://www.ugg-boots.net.co
http://www.raybansunglassessales.us.com
http://www.doudounemonclerpascher.fr
http://www.coachcanadaoutlet.ca
http://www.coachoutletonlinecoachfactoryoutlet.us.com
http://www.ralphlaurensaleclearance.org.uk
http://www.cheap-uggboots.net.co
http://www.uggsaustralia.fr
http://www.cheapraybans.com.co
http://www.ugg-outlet.com.co
http://www.coachoutletcoachfactoryoutletonline.us.com
http://www.canadagooseoutlet.net.co
http://www.christianlouboutinsale.us.org
http://www.oakley.nom.co
http://www.michaelkorshandbags-outlet.us.org">http://www.michaelkorshandbags-outlet.us.org
http://www.coachfactory-outletstore.us.com
http://www.uggsoutlets.com.co
http://www.yeezyboost-350.us.com
http://www.uggsandals.in.net
http://www.birkenstockshoes.us.com
http://www.pandoracharms.name
http://www.guccioutlets.us.org
http://www.birkenstockoutletstore.us.com
http://www.michael-kors-outletonline.us.com">http://www.michael-kors-outletonline.us.com
http://www.cheapjordan-shoes.in.net
http://www.uggsoutlets.in.net
http://www.harden-vol1.com
http://www.jameshardenshoes.us.com
http://www.uggs-outlets.in.net
http://www.pandora-jewelryoutlet.us.com
http://www.ralphlaurenuk.me.uk
http://www.cheapuggboots.com.co
http://www.ultraboost.us
http://www.coach-outlets.us.com
http://www.michaelkors-mkoutlet.in.net">http://www.michaelkors-mkoutlet.in.net
http://www.michaelkorsoutlets.us.com">http://www.michaelkorsoutlets.us.com
http://www.monclerjacketsoutlet.in.net
http://ugg.bootsoutlet.us.com
http://www.adidasyeezyshoes.us.com
http://www.timberland-outlets.us
http://www.pandora-outlet.us.com
http://www.nike-store.us.com
http://www.coach-outlet.in.net
http://www.adidas-nmd.us.com
http://www.adidasnmd.org.uk
http://www.ugg.in.net
http://www.toryburchoutletonline.com.co
http://www.adidas-outlet.us.com
http://www.michaelkorsoutletstoreonline.us.org">http://www.michaelkorsoutletstoreonline.us.org
http://www.cheapmlbjerseys.us
http://www.polo-ralphlauren.in.net
http://www.burberrysale.org.uk
http://www.northfaceoutlet.us.com
http://www.superdryuk.org.uk
http://www.coach-factoryoutlet-online.us.com
http://www.michae

What do you think?
name from url
   can't read the code? (reload)