A Really Simple Demo
The slideshow above uses default settings, and is initialised with one line of code, shown here attached to the jQuery document ready event:
$(document).ready(function () {
$('#slideshow-div').rsfSlideshow();
});
By default, the slideshow looks for slide data within the markup. Here's how the HTML looks:
<div id="slideshow-div" class="rs-slideshow"> <!-- Set up an initial slide -- this provides an image for users without JavaScript --> <div class="slide-container"> <img src="demo/images/morzine-2011-c.png" alt="The first image in a slideshow demo." title="This is the first slide" /> </div> <!-- This list contains data about each slide. So that the slide images aren't loaded with the page, we use <a> tags. With some extra CSS rules, this allows for users without JavaScript to access the images by clicking the links. --> <ol class="slides"> <li> <a href="demo/images/morzine-2011-c.png">morzine-2011-c.png</a> </li> <li> <a href="demo/images/morzine-2011-a.png">morzine-2011-a</a> </li> <li> <a href="demo/images/morzine-2011-b.png">morzine-2011-a</a> </li> </ol> </div>
And the CSS:
.rs-slideshow {
border: 12px solid #444;
height: 240px;
margin: 24px auto;
overflow: hidden;
position: relative;
width: 620px;
}
.rs-slideshow .slide-container {
background-color: #444;
height: 100%;
left: 0;
overflow: hidden;
position: absolute;
top: 0;
width: 100%;
}
.rs-slideshow .slide-container img {
position: absolute;
}
/* Hide the slide data container */
.rs-slideshow .slides {
display: none;
}
Note: The demos on this page use the CSS3 border-radius property. In order to keep the demo code on this page simple and easy to read, these CSS rules aren't shown here. If you download the demo code, you'll find this extra code there.
Captions and Links
Captions can be added and slide images can be made clickable, simply by adding to the HTML. This can also be done programatically—we'll get to that later.
Notice the changes to the HTML:
<div id="slideshow-div"> <!-- Set up an initial slide -- this provides an image for users without JavaScript. Notice that we've also added the caption for the first slide. --> <div class="slide-container"> <img src="demo/images/morzine-2011-c.png" alt="The first image in a slideshow demo." title="This is a caption for the first slide" /> <span class="slide-caption">This is a caption for the first slide</span> </div> <!-- We can add optional captions and links for each slide directly to the markup. (This can also be done programatically, using the API—see below for details) --> <ol class="slides"> <li> <a href="demo/images/morzine-2011-c.png" title="This is a caption for the first slide">This is a caption for the first slide</a> </li> <li> <a href="demo/images/morzine-2011-a.png" title="This is a caption for the second slide, and this slide is clickable!" data-link-to="http://reallysimpleworks.com">This is a caption for the second slide</a> </li> <li> <a href="demo/images/morzine-2011-b.png" title="This is the third slide">This is a caption for the third slide</a> </li> </ol> </div>
Some additional CSS for the caption element:
.slide-container .slide-caption {
background-color: #000;
bottom: 0;
color: #fff;
display: block;
left: 0;
padding: 6px 12px;
position: absolute;
text-align: center;
right: 0;
filter: alpha(opacity=70);
-khtml-opacity: 0.7;
-moz-opacity: 0.7;
opacity: 0.7;
}
Adding Controls
The slideshow below uses same HTML markup and CSS as the previous examples (with a couple of extra slides), but initiates the slideshow with some additional options to add controls.
Here's how this is done:
$('#slideshow-controls').rsfSlideshow({
controls: {
playPause: {auto: true},
previousSlide: {auto: true},
nextSlide: {auto: true},
index: {auto: true}
}
});
For full details on using the controls options, please see the controls reference section.
Basic Customisation
The slideshow below uses exactly the same HTML markup and CSS as the previous example, but initiates the slideshow with some additional options.
Here's how this is done:
$('#slideshow-div').rsfSlideshow({
interval: 3,
transition: 500,
effect: 'slideLeft'
});
- Setting the
intervalproperty to 3 means that each slide is shown for 3 seconds. - Setting the
transitionproperty to 500 means that the transition between two slides takes 500 milliseconds. - Setting the
effectproperty to'slideLeft'makes slides transition by sliding from right to left.
Effect Sequences
In the previous example, the effect property is a string, specifying a single transition effect for our slideshow. We can, however, use an object instead, which allows us to create a sequence of transition effects. The slideshow below demonstrates this.
Here's the code:
$('#slideshow-div').rsfSlideshow({
interval: 3,
transition: 500,
effect: {
effects: Array('slideUp', 'slideLeft', 'slideRight'),
iteration: 'backAndForth'
}
});
Notice that the effect property is now an object with two properties of its own: effects and iteration.
effects is an array of transition effects, and iteration determines the way in which we loop through these effects.
There are three possible values for iteration:
'loop'causes the slideshow to loop through the specified array of effects, starting from the beginning when the last one is reached.'backAndForth'will move back and forth through the array of specified effects, changing direction at each end.'random'will randomly select an effect from the specified array on each transition.
See the transition effects reference for more on transition effect settings.
Adding Slides Programatically
So far, we've used markup to pass slide data to the slideshow, but there's another way to do this.
The HTML is much neater, as we only need to set up the initial slide for non-JavaScript users:
<div id="slideshow-div"> <!-- Set up an initial slide -- this provides an image for users without JavaScript. --> <div class="slide-container"> <img src="demo/images/morzine-2011-c.png" alt="The first image in a slideshow demo." title="This is the first slide" /> <span class="slide-caption">This is a caption for the first slide</span> </div> </div>
We then add slides using JavaScript. Here's how the code looks:
// Create an array of slide objects
var slides = Array(
{
url: 'demo/images/morzine-2011-c.png',
caption: 'This is a caption for the first slide'
},
{
url: 'demo/images/morzine-2011-a.png',
caption: 'This is a caption for the second slide, and this slide is clickable!',
link_to: 'http://reallysimpleworks.com'
},
{
url: 'demo/images/morzine-2011-b.png',
caption: 'This is a caption for the third slide'
}
);
// Add the slides on initialisation
$('#slideshow-div').rsfSlideshow({slides: slides});
This has exactly the same effect as the very first example above.
If you watch the slideshow above, you'll notice that there are actually five slides. The additional two are added later, using the API. Here's how:
// Create the new slide objects
var slides = Array(
{
url: 'demo/images/lancs-yorks.png',
caption: 'This is a caption for the fourth slide'
},
{
url: 'demo/images/cragg-road.png',
caption: 'This is a caption for the fifth slide'
}
);
// Add the new slides using the API
$('#slideshow-div').rsfSlideshow('addSlides', slides);
This demonstrates adding multiple slides, contained within an array, but you can also pass a single slide object—or even to just an image URL—to the addSlides method.
We can add slides to the end of the slideshow at any time using this method, which allows for the loading and insertion of additional slides at a later time.
Options
Options are set by passing them as an object on initialisation. For example:
$('#slideshow-div').rsfSlideshow({
interval: 3,
transition: 500,
slides: slides
});
-
intervalThe length of time that each slide is shown for, in seconds.
Default:
5 -
transitionThe length of time taken for transition between slides, in milliseconds (this has no effect if the
effectproperty is set to'none').Default:
1000 -
effectThe effect(s) used to transition between slides. This can be a string specifying the transition effect:
effect: 'slideLeft'
Or an array of strings specifying one or more transition effects (an effect is randonly selected from the array on each transition):
effect: Array('slideLeft', 'slideUp', 'slideRight')Or an object with two properties:
effect: { effects: Array('slideLeft', 'slideUp', 'slideRight'), iteration: 'backAndForth' }effectsis an array of transition effects;iterationspecifies how the next effect is selected, and can be one of:'random','loop'or'backAndForth'. See this example for a more detailed explanation of these settings.See the transition effects reference for details of the available effect and iteration settings.
Default:
'fade' -
easingThe jQuery easing parameter used for slide transitions. To add more easing options, use the easing jQuery plugin.
Default:
'swing' -
loopWhen true, the slideshow is circular—the first slide follows the last, and the last slide is previous to the first.
Default:
true -
autostartWhen true, the slideshow will automatically start on initialisation. If false, the slideshow must be started using the
startShowmethod.Default:
true -
slidesAn array of slide objects to be passed to the slideshow on initialisation. See the Slide Object section for more.
Default:
Array()(empty array) -
slide_container_classThe name of the class used for the individual slide containers.
Default:
'slide-container' -
caption_container_classThe name of the class added to slide caption
<span> tags.Default:
'slide-caption' -
data_containerA jQuery selector for the container of the slides data (only used if embedding slide data in the markup).
If the selector begins with
'#', the element can be placed anywhere within the document body. If not, this element is assumed to be a direct child of the element that the slideshow is applied to.Default:
'ol.slides' -
slide_data_containerA jQuery selector for the container of data for an individual slide (only used if embedding slide data in the markup).
Default:
'li' -
slide_data_selectorsAn object containing information about where the Really Simple™ Slideshow plugin can find slide data embedded within elements specified by the
slide_data_containersetting (see above). Used to customise the markup for embedding slide data.Each slide property can be assigned a jQuery selector and attribute. The default (see below), for example, will find each slide URL within the
hrefattribute of the<a>tag inside of the specifiedslide_data_containerselector.Default:
{ url: {selector: 'a', attr: 'href'}, caption: {selector: 'a', attr: 'title'}, link_to: {selector: 'a', attr: 'data-link-to'}, effect: {selector: 'a', attr: 'data-effect'} } -
eventHandlersAn object containing event handlers to bind on slideshow initialisation.
Any jQuery event can be used, as can any of the Really Simple™ Slideshow events.
Note: this option is intended for the customisation of default behaviour—you can also bind handlers to slideshow events using jQuery's bind() method. See events reference for details
Default:
{ rsStartShow: function(rssObj, e) { var settings = $(rssObj).data('rsf_slideshow').settings; $('.' + settings.play_pause_class + '[data-control-for="' + $(rssObj).attr('id') + '"]').html('Pause').addClass('rss-playing'); }, rsStopShow: function(rssObj, e) { var settings = $(rssObj).data('rsf_slideshow').settings; $('.' + settings.play_pause_class + '[data-control-for="' + $(rssObj).attr('id') + '"]').html('Play').addClass('rss-paused'); } } -
controlsSettings for generated slideshow controls, such as play/pause, previous, next, and indexing.
Basic use of these options looks like this:
$('#slideshow-div').rsfSlideshow({ controls: { playPause: {auto: true}, // auto-generate a play/pause control previousSlide: {auto: true}, // auto-generate a "previous slide" control nextSlide: {auto: true}, // auto-generate a "next slide" control index: {auto: true} // auto-generate a numbered index control } });This switches on the auto-generation of each control. You can, of course, leave any of these out (they are off by default) to customise the control functionality for a slideshow.
There are a few other basic settings available for these options:
$('#slideshow-div').rsfSlideshow({ controls: { playPause: { auto: true, // auto-generate a play/pause control // specify the class to add to the play/pause // control when the slideshow is playing playing_class: 'rs-playing', // specify the class to add to the play/pause // control when the slideshow is paused paused_class: 'rs-paused' }, previousSlide: { auto: true, // auto-generate a "previous slide" control // automatically pause the slideshow when // this control is clicked autostop: true }, nextSlide: { auto: true, // auto-generate a "next slide" control // automatically pause the slideshow when // this control is clicked autostop: true }, index: { auto: true, // auto-generate a numbered index control // specify the class to add to an index link // when its corresponding slide is showing active_class: 'rs-active', // automatically pause the slideshow when // this control is clicked autostop: true } } });To make the auto-generated control features as customisable as possible, several default methods for generating controls, and locating and placing them within the DOM, can be overridden by specifying options. Each control (
'playPause','previousSlide','nextSlide'and'index') has three options for customising this behaviour, as follows (demonstrated here using theplayPausecontrol):$('#slideshow-div').rsfSlideshow({ controls: { playPause: { // Returns the control to be added to the DOM generate: function($slideshow) {}, // Places the control into the DOM place: function($slideshow, $control) {}, // Find the control within the DOM and return as a jQuery object get: function($slideshow) {}, } });Each of these methods recieves the current slideshow instance as a jQuery object, and the method specified in the
placeoption also recieves the control element(s), as returned by the method specified in thegenerateoption.The
indexcontrol type has the following additional options:$('#slideshow-div').rsfSlideshow({ controls: { index: { // Find the set of index controls and returns them as a jQuery object getEach: function($slideshow) {}, // Find a single index control by slide key and return as a jQuery object getSingleByKey: function($slideshow, slide_key) {}, // Takes a control item from the set—as returned by getEach()— // and extracts the key of the slide it corresponds to) getSlideKey: function($controlItem) {}, } });
Methods
API methods can be called on any initialised slideshow as follows:
$('#slideshow-div').rsfSlideshow('methodName', parameters);
-
addSlidesAdd one or more slides to the end of the slideshow. See The Slide Object for more details.
var slides = Array( { url: 'slide-image-url.png', caption: 'Slide catpion text', // (optional) link_to: 'url-to-link-slide-to', // (optional) effect: 'slideUp' // (optional) }, { url: 'another-slide-image-url.png' }, ... ) $('#slideshow-div').rsfSlideshow('addSlides', slides); -
removeSlidesRemove slides from the slideshow.
Called without any arguments, this method will remove all slides from the current slideshow.
$('#slideshow-div').rsfSlideshow('removeSlides');You can also pass an integer, or an array of integers, to the method in order to remove specific slides at given positions in the slides array:
// Remove the slide at key 2 (the third slide) $('#slideshow-div').rsfSlideshow('removeSlides', 2); // Remove the slides at keys 1, 4 and 6 (the 2nd, 5th and 7th slides) $('#slideshow-div').rsfSlideshow('removeSlides', [1,4,6]);Note that when slides are removed the slides are repositioned within the array. The following code, for example, will remove two slides from the beginning of the slides array:
$('#slideshow-div').rsfSlideshow('removeSlides', 0); // The second slide has now become the first, and is removed by the next line $('#slideshow-div').rsfSlideshow('removeSlides', 0);Slide keys passed within an array, however, will only be removed once. The following code will only remove the first slide:
$('#slideshow-div').rsfSlideshow('removeSlides', [0,0]);It's a good idea to call the
stopShowmethod before removing slides, in order to prevent errors where slide data is not found. -
getSlideDataReturns a slide object or the entire array of slide objects
// Return the entire slides array $('#slideshow-div').rsfSlideshow('getSlideData'); // Return the slide object for the third slide $('#slideshow-div').rsfSlideshow('getSlideData', 2); -
startShowStart the slideshow. By default, the slideshow will start automatically on initialisation—to prevent this behaviour, use the
autostartoption.$('#slideshow-div').rsfSlideshow('startShow'); -
stopShowStop the slideshow.
$('#slideshow-div').rsfSlideshow('stopShow'); -
toggleShowStarts the slideshow if currently stopped, or stops the slideshow if currently running.
$('#slideshow-div').rsfSlideshow('toggleShow'); -
nextSlideLoad and transition into the next slide in the slideshow.
$('#slideshow-div').rsfSlideshow('nextSlide'); -
previousSlideLoad and transition into the previous slide in the slideshow.
$('#slideshow-div').rsfSlideshow('previousSlide'); -
goToSlideLoad and transition into the slide at the provided key in the slides array.
var slide_key = 3; $('#slideshow-div').rsfSlideshow('goToSlide', slide_key); -
currentSlideKeyReturns the array key of the current slide.
$('#slideshow-div').rsfSlideshow('currentSlideKey'); -
totalSlidesReturns the total number of slides currently in the slides array.
$('#slideshow-div').rsfSlideshow('totalSlides'); -
isRunningReturns
trueif the slideshow is running,falseif not.$('#slideshow-div').rsfSlideshow('isRunning'); -
addControlGenerates a control for the slideshow and adds it to the DOM, according to settings specified as options.
$('#slideshow-div').rsfSlideshow('addControl', type);typemust be one of:'playPause','previousSlide','nextSlide'or'index'.Note: only use this method if you haven't set controls to auto-generate using options.
-
bindPlayPauseBinds an element(s) on the page to play/pause functionality for the slideshow.
Note: when controls are auto-genreated, this is taken care of. Only use this method when you're creating your own controls on the page, and need to bind this functionality to them.
$('#slideshow-div').rsfSlideshow('bindPlayPause', $playPause);$playPause(required) is the play/pause control element to bind the behaviour to, as a jQuery object. -
bindPreviousBinds an element(s) on the page to "previous slide" functionality for the slideshow.
Note: when controls are auto-genreated, this is taken care of. Only use this method when you're creating your own controls on the page, and need to bind this functionality to them.
$('#slideshow-div').rsfSlideshow('bindPrevious', $prev, autostop);$prev(required) is the "previous slide" control element to bind the behaviour to, as a jQuery object. Theautostopparameter is optional, and if true (default) causes the slideshow to pause whenever the control is clicked. -
bindNextBinds an element(s) on the page to "next slide" functionality for the slideshow.
Note: when controls are auto-genreated, this is taken care of. Only use this method when you're creating your own controls on the page, and need to bind this functionality to them.
$('#slideshow-div').rsfSlideshow('bindNext', $next, true);$next(required) is the "next slide" control element to bind the behaviour to, as a jQuery object. Theautostopparameter is optional, and if true (default) causes the slideshow to pause whenever the control is clicked. -
bindIndexBinds an element(s) on the page to "go to slide x" functionality for the slideshow.
Note: when controls are auto-genreated, this is taken care of. Only use this method when you're creating your own controls on the page, and need to bind this functionality to them.
$('#slideshow-div').rsfSlideshow('bindIndex', $index, true);$index(required) is the set of index control elements to bind the behaviour to, as a jQuery object. Theautostopparameter is optional, and if true (default) causes the slideshow to pause whenever the an index control is clicked.If the ID of your slideshow element is 'my-slideshow' your "slide x" controls should be marked up as follows:
<a href="#" class="rs-next" data-control-for="my-slideshow" data-slide-key="0">1</a> <a href="#" class="rs-next" data-control-for="my-slideshow" data-slide-key="1">2</a> <a href="#" class="rs-next" data-control-for="my-slideshow" data-slide-key="2">3</a> ...etc...
This expected markup can be customised by assigning your own methods within the
index: {getEach: function() {...your method here...}}andindex: {getSlideKey: function() {...your method here...}}properties in the controls options.
Transition Effects
The effect setting is a string, an array, or an object (see the effect option reference for details). Currently, the valid effect names are as follows:
'fade'— fade from one slide to the next'slideLeft'— slide the previous slide out and the new slide in, from right to left'slideRight'— as above, from left to right'slideUp'— as above, upwards'slideDown'— as above, downwards'none'— no effect; slides switch instantly from one to the next
When passing an object as the effect setting, the second parameter, iteration, specifies the way in which the transition effects are selected for use. Here are the available options:
'random'— an effect is randomly selected from the specified array of effect names'loop'— the specified array of effect names is looped through, starting again at the first once the end of the array is reached'backAndForth'— the specified array of effect names is iterated through forwards, and when the end is reached, is iterated through backwards, until back at the start of the array, where this process begins again
See this demonstration for a working example of the iteration parameter in use.
The Slide Object
The slide object is used to add a slide to the slideshow. It has one required property, and some optional ones:
var slide = {
// The image URL (required)
url: 'url-of-slide-image.png',
// The text for the slide caption (optional)
caption: 'Caption text goes here',
// The URL to link the image to when clicked (optional)
link_to: 'url-to-link-to',
// A string specifying the effect to use when transitioning to this slide (optional)
// This overrides the global effect settings for the slideshow
effect: 'slideLeft'
};
Slideshow Events
Really Simple™ Slideshow triggers various events, each of which can have handlers bound to them, just like any jQuery event:
$('#my-slideshow').bind('rsStartShow', function() {
alert('The slideshow has started!');
});
The following slideshow events are currently supported:
'rsStartShow'— triggered whenever the slideshow is started'rsStopShow'— triggered whenever the slideshow is stopped'rsPreTransition'— triggered when the slide is requested, but before the slide image has loaded'rsImageReady'— triggered when the image for the slide has loaded'rsPostTransition'— triggered when the transition has completed, and the slide is in place'rsLastSlide'— as'rsPostTransition'but only triggered on the last slide of the slideshow'rsFirstSlide'— as'rsPostTransition'but only triggered on the first slide of the slideshow
Download
- Download Zip File (includes demo, CSS, etc.)
- Minified source code (unless you're hacking the plugin's source, use this version)
- Raw source code
- GitHub
Issues
Please report bugs, etc. using GitHub.
Browser Support
We've tested Really Simple™ Slideshow in the latest versions of Chrome, Safari, Opera and Firefox, and IE versions 6–9.
License
Really Simple™ Slideshow is released under the MIT license and is free to use for both commercial and non-commercial projects.
Change Log
Version 1.4.8 — 5 Jul 2011
- All public methods now return a jQuery collection (unless they return specific data).
- QUnit test suite updated to include tests for all public methods.
Version 1.4.7 — 30 Jun 2011
- Improved the handling of the removal of outgoing slide elements.
- Moved all transition effects into the
$.refSlideshow.transitionsobject, allowing for extension of the slideshow effects. - Added an asynchronous method for determining image dimensions, which prevents an occasional race condition in Chrome (previously caused the slideshow not to start in Chrome, in certain situations).
- Added a CSS rule to fix the broken fade transition effect in IE8.
Version 1.4.6 — 1 Jun 2011
- Added support for optional image alt and title parameters (requires custom options). [Thanks to Richard Dean for this contribution]
Version 1.4.5 — 27 May 2011
- Added HTML cpations demo.
- Accommodated for HTML captions.
- Changed caption container element from span to div.
- Removed the broken controlsdemo.html
Version 1.4.4 — 18 May 2011
- Fixed bug causing fatal "Object doesn't support this..." error in IE 6/7/8.
Version 1.4.3 — 17 May 2011
- Fixed bug causing the play/pause control to start in the "playing" state when the autostart setting is set to false.
Version 1.4.2 — 12 May 2011
- Fixed fatal bug introduced in previous update.
- Improved image width detection to fix occasional image positioning bug in certain browsers.
Version 1.4.1 — 12 May 2011
- Renamed the 'private' object to 'RssPrivateMethods' to avoid problems with the global namespace.
- Tidy up of the code and correction of some minor errors.
Version 1.4 — 5 May 2011
- Added the
removeSlides()method. - Added the
getSlideData()method. - Included QTest unit testing (for new methods only).
Version 1.3.1 — 2 May 2011
- Fixed image size detection bug when resizing images using CSS.
- Fixed bug causing controls to be auto-generated regardless of settings.
- Private methods are no longer available publicly.
- Moved all default options to
$.rsfSlideshow.defaults
Version 1.3 — 1 May 2011
- Added the
controlsoption. - Changed the name of the
eventCallbacksoption toeventHandlers. - Changed the behaviour of the following methods:
bindPlayPause(),bindPreviousSlide(),bindNextSlide()andbindIndex()so that they require jQuery objects to bind the behaviours to. - Changed the name of the
bindNext()method tobindNextSlide(). - Changed the name of the
bindPrevious()method tobindPreviousSlide(). - Removed the following options:
play_pause_class,prev_class,next_class,stop_on_prev_next,index_classandstop_on_index. - Added auto-generation of control elements.
Version 1.2 — 27 Apr 2011
- Added
bindIndex()method as a quick way to create "go to slide..." control functionality. - Added
bindPrevious()andbindNext()methods as a quick way to create prev/next control functionality. - Added
bindPlayPause()method as a quick way to create play/pause control functionality. - Added the
isRunning()method to determine whether the slideshow is running. - Added default event callbacks to options, to aid with setup of global slideshow functionality.
- Added
$.rsfSlideshow()method for setting options globally. - Added event triggers for slideshow events.
Version 1.1 — 18 Apr 2011
- Added a minified version of the source.
- Changed the name of the plugin file to
jquery.rs.slideshow.jsto bring it into line with jQuery plugin naming convention. - Bug fix: More consistent behaviour across browsers when loading cached images into a slideshow.
- Changes to the default markup used for specifying slide data. Markup now makes more sense semantically, and offers the opportunity to allow non-JavaScript users to click on links to slideshow images. (Extra CSS is required to do this, and we'll publish this soon).
slide_data_selectorsoption added. This makes slide data more customisable.url_data_selectoroption removed (replaced byslide_data_selectors).caption_data_selectoroption removed (replaced byslide_data_selectors).link_url_data_selectoroption removed (replaced byslide_data_selectors).effect_data_selectoroption removed (replaced byslide_data_selectors).