Easily extendable, class-based, slideshow widget. Use any element, not just images. Comes with packaged transitions but is easy to extend and create your own transitions. The class is built to handle the basics of a slideshow, extend it to implement your own navigation piece and custom transitions.
Options, Events, Loop
SlideShow can get the transition information from the class attribute of your slide which will take precedence over what you've defined in your options. It will only grab the immediate children of the container (slideshow in this case). You can put whatever element type you want as the slides, and put anything inside of the slides.
#HTML
<div id="slideshow">
<div class="transition:crossFade duration:1000">1</div>
<div>2</div> <!-- gets default transition/duration -->
<div class="transition:blindRightFade duration:400">3</div>
<div class="transition:fade duration:1000">4</div>
<div class="transition:pushUp duration:2000">5</div>
<div class="transition:pushDown duration:500">6</div>
</div>
Just pass in the slideshow container element to the constructor (and a few options, if you'd like) and you're set.
#JS
mySlideShow2 = new SlideShow('slideshow');
// or
mySlideShow = new SlideShow('slideshow',{
delay: 2000,
transition: 'fade',
duration: 500,
autoplay: true
});
By default, autoplay is false and you can control the slide show.
// show the 4th slide (pass it the index if you know it)
mySlideShow.show(3);
// or pass it an element
mySlideShow.show(mySlideShow.slides[3]);
var el = $('someSlide');
mySlideShow.show(el);
SlideShow implements Loop (also on the forge) so it inherits startLoop and stopLoop. SlideShow creates aliases for these as play and pause.
mySlideShow.play();
// later
mySlideShow.pause();
If you wanted a navigation piece you could do something like:
var slideLabels = $$('some-elements-in-the-same-order-as-the-slides');
slideLabels.each(function(el, index){
el.addEvent('click',function()
mySlideShow.show(index);
});
});
SlideShow will set the position of your container to relative if it is not already positioned absolute. It will also set all of your slides to position: absolute, display: block, and z-index: 0. Because they are positioned absolutely you'll need to give them a width. Also, you'll usually want to set the container overflow to hidden in your CSS.
#CSS
div#slideshow {
width: 500px;
height: 280px;
overflow: hidden;
}
div#slideshow div {
width: 500px
height: 280px;
}
var mySlideShow = new SlideShow(element, options);
7000) Milliseconds between slide transitionscrossfade) Default transition for slides with none specified in the class attribute.7000) Default transisition duration for slides with none specified in the class attribute.false) Calls play method in the contructor if false.onShow(slideData)
mySlideShow.addEvent('show',function(slideData){
slideData.next.element; // the actual DOM element
slideData.next.index; // the index of the element in the slideshow
slideData.previous.element;
slideData.prevoius.index;
});
onShowComplete(slideData)
onShow.Shows the slide passed in as an argument
mySlideShow.show(slide, options);
This SlideShow instance.
mySlideShow.show(2); // index
mySlideShow.show(mySlideShow.slides[2]); // element reference
// change the duration and transition
mySlideShow.show(1, {
duration: '4000',
transition: 'pushLeft'
});
Shows the next slide
mySlideShow.showNext(option);
This SlideShow instance.
Shows the previous slide
mySlideShow.showPrevious(options);
This SlideShow instance.
Allows you to change the options on the fly
mySlideShow.resetOptions(obj);
mySlideShow.resetOption({
duration: 1000,
transition: 'fadeThroughBackground'
});
This SlideShow instance.
Reverses the autoplay.
mySlideShow.reverse();
mySlideShow.play();
mySlideShow.reverse(); // going backwards now
mySlideShow.reverse(); // going forward now
This SlideShow instance.
Custom Native to allow all of its methods to be used with any DOM element via the dollar function $.
Element shortcut method to create a slideshow instance with this element.
$('slide-container').playSlideShow(options);
autoplay will be ignored.Element shortcut method to pause a slideshow instance created with the playSlideShow method.
$('slide-container').pauseSlideShow();
Adding transitions is a snap. The Class itself has an add function that takes two arguments: the name of the transition, and the function to control it.
SlideShow.add(function(previous, next, duration, instance){
// do stuff
});
function(previous, next, duration, instance)
previous is the previous slide element referencenext is the next slide element referenceduration is how long the transition should last.instance is the instance of SlideShow, handy to find the size of the container (instance.element) or any other information.When writing your own transitions there are a few things to note:
z-index is 1 so it's on top.z-index is 0 so it's behind.top: 0 and left:0, so you'll need to reposition next for any fancy movement.display:noneduration is met, the previous slide will be reset to display: none, top:0, left:0.SlideShow.add('fade', function(previous, next, duration, instance){
previous.set('tween',{duration: duration}).fade('out');
return this;
});
Pretty simple. Since the next slide is directly behind the previous, we can just fade out the previous slide and there's our new one.
SlideShow.add('blindLeft', function(previous, next, duration, instance){
var distance = instance.element.getStyle('width').toInt();
next.setStyles({
'left': distance,
'z-index': 2
}).set('tween',{duration: duration}).tween('left', 0);
return this;
});
A bit more complicated. First we have to measure the distance our slide needs to travel, then we set it's left style to be totally out of the slideshow view and change it's z-index from 0 to 2 so it's above the previous slides z-index: 1. Once it's all setup we just tween left back to 0. Our slide smoothly slides over the the previous slide.
SlideShow.add('blindLeftFade',function(p, n, d, i){
this.blindLeft(p,n,d,i).fade(p,n,d,i);
});
this references the object containing all of the transitions so you can chain effects.