- Overview
- Documents
- Demos
Cycle2 is a versatile slideshow plugin for jQuery built around ease-of-use. It supports a declarative initialization style that allows full customization without any scripting. Simply include the plugin, declare your markup, and Cycle2 does the rest.
Highlights:
- Supports all browsers
- Declarative: no scripting needed!
- Responsive: fully control your slideshow via css (resize this page to see!)
- Customizable: per-slide option overrides
- Extensible: fully customizable API (on a per-slideshow basis if you wish)
- Smart: supports image loading and delayed initialization
- Smarter: supports progressive image loading
- Out-of-the-box functionality for pagers, captions, overlays, and prev/next controls
- Support for swipe gesture on mobile devices
- Fade, scroll, shuffle, tile and carousel transitions
- Bookmarkable slides
- And lots more!
How it Works
It's very simple really, even your grandmother could use it. It goes like this:
- Include jQuery and the Cycle2 plugin on your page.
- Declare your slideshow markup as shown in the numerous demos.
- Sit back and enjoy your slideshow while sipping a cool beverage.
Need more details? Ok, here's the deal: Cycle2 provides a plugin function calledcycle. That function is invoked on a container element. Each image in the container becomes a "slide". Options control how and when the slides are transitioned. But here's the thing... you don't have to call cycle and pass your options. You declare your options as attributes on the slideshow element and Cycle2 grabs them from there.
What's that you say? You'd rather set your options via script than in the markup? Ok fine, have it your way. You don't have to declare your options in the markup (but it's fun!), you can invoke cycle from your own code and pass your options in the old-fashioned way. You can even grab hold of the API object and inject your own implementation of any of the API methods that you choose.
Example
All that is needed is to include jQuery and the Cycle2 plugin using script tags like this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="http://malsup.github.com/jquery.cycle2.js"></script>
markup like this:
<div class="cycle-slideshow"> <img src="http://malsup.github.io/images/p1.jpg"> <img src="http://malsup.github.io/images/p2.jpg"> <img src="http://malsup.github.io/images/p3.jpg"> <img src="http://malsup.github.io/images/p4.jpg"> </div>
The Javascript
$( '.mySlideshows' ).cycle();
Options
Options are set on the slideshow element using data-cycle-option-name attributes. The option name is always lowercase and prefixed with data-cycle-. For example, to set the speedand manual-speed options you would do this:
<div class="cycle-slideshow" data-cycle-speed="600" data-cycle-manual-speed="100"> ...
This is the preferred method of setting slideshow options as it greatly reduces (or eliminates) the need for custom initialize script.
Options can also be set programatically if you prefer, by passing an options hash to the cycle method. When setting options programmically the option name does not contain thedata-cycle- prefix and the string is camelCased on hyphens:
$('.cycle-slideshow').cycle({ speed: 600, manualSpeed: 100 });
All options can be declared with data-cycle-* attributes on the container element and will be inherited by all slides.
Options in the table below that have a • symbol can be overridden on individual slide elements in order to have specific properties for specific slides.
Name | Type | Default Value | Description | |
---|---|---|---|---|
allow-wrap | boolean | true |
This option determines whether or not a slideshow can advance from the last slide to the first (or vice versa). If set to false then once a slideshow reaches its last slide it can no longer be advanced forward, it can only advance backward. Likewise, when a slideshow is displaying the first slide it can not advance backward.
By default a slideshow will advance seamlessly from the last slide to the first. |
|
auto-height | integer or ratio | 0 |
This option determines whether or not Cycle2 will provide height management for the slideshow which can be very useful in fluid or responsive designs. There are three ways this option can be used:
To force the slideshow container to a specific aspect ratio, for example to hold a set of images that are 600x400, use a ratio string like this: data-cycle-auto-height="600:400" To disable height management, set this option's value to -1 or false. See Auto Height demo for more info. |
|
autoSelector (programmatic use only) |
jQuery selector | '.cycle-slideshow[data-cycle-auto-init!=false]' |
A jQuery selector which identifies elements that should be initialized automatically by Cycle2. The default value is .cycle-slideshow. Add the cycle-slideshow class to your slideshow container and Cycle2 will automatically find and initialize it when the DOM ready event fires.
This value can only be changed programmatically and it can not be changed after the DOM ready event has fired. To change, include script like this after Cycle2 has loaded and before the ready event fires:
$.fn.cycle.defaults.autoSelector = '.mySlideshows'; |
|
caption | jQuery selector | '> .cycle-caption' | A selector which identifies the element that should be used for the slideshow caption. By default, Cycle2 looks for an element with the class .cycle-caption that exists within the slideshow container. | |
caption-template | • | string | '{{slideNum}} / {{slideCount}}' |
A template string which defines how the slideshow caption should be formatted. The template can reference real-time state information from the slideshow, such as the current slide index, etc. Cycle2 uses simple Mustache-style templates by default. |
continueAuto (programatic use only) |
boolean | undefined | Option which can be set dynamically to instruct C2 to stop transitioning on a timeout. This is useful when you want to start with an automatic slideshow but later switch to a manual slideshow. This option can also be a function which returns a boolean value. | |
delay | integer | 0 | The number of milliseconds to add onto, or substract from, the time before the first slide transition occurs. | |
disabled-class | string | disabled |
The classname to assign to prev/next links when they cannot be activated (due to data-cycle-allow-wrap="false".
The value of this option should not include a preceding "dot".
|
|
easing | • | string | null | Name of the easing function to use for animations. |
fx | • | string | 'fade' | The name of the slideshow transition to use. The following transition names are available by default and more can be added with plugins: fade, fadeout, none, and scrollHorz. |
hide-non-active | boolean | true | Determines whether or not Cycle2 hides the inactive slides. | |
loader | true | false | "wait" | false |
The loader option sets the image loader behavior for the slideshow. Example.
|
|
log | boolean | true | Set to false to disable console logging. | |
loop | integer | 0 |
The number of times an auto-advancing slideshow should loop before terminating. If the value is less than 1 then the slideshow will loop continuously. Set to 1 to loop once, etc.
Setting the allow-wrap option to false will override looping.
|
|
manual-speed | • | integer | undefined | The speed (in milliseconds) for transitions that are manually initiated, such as those caused by clicking a "next" button or a pager link. By default, manual transitions occur at the same speed as automatic (timer-based) transitions. |
manual-trump | boolean | true | Determines whether or not transitions are interrupted to begin new ones if the new ones are the result of a user action (not timer) | |
next | jQuery selector | '> .cycle-next' | A selector string which identifies the element (or elements) to use as a trigger to advance the slideshow forward. By default, Cycle2 looks for an element with the class .cycle-next that exists within the slideshow container. | |
next-event | jQuery selector | 'click.cycle' | The event to bind to elements identified with the next option. By default, Cycle2 binds click events. | |
overlay | jQuery selector | '> .cycle-overlay' | A selector string which identifies the element to use as the overlay element. A slideshow overlay typically provides information about the current slide. By default, Cycle2 looks for an element with the class .cycle-overlay that exists within the slideshow container. | |
overlay-template | • | string | '<div>{{title}}</div><div>{{desc}}</div>' |
A template string which defines how the overlay should be formatted. The template can reference real-time state information from the slideshow, such as the current slide index, etc. Cycle2 uses simple Mustache-style templates by default. |
pager | jQuery selector | '> .cycle-pager' | A selector string which identifies the element to use as the container for pager links. By default, Cycle2 looks for an element with the class .cycle-pager that exists within the slideshow container. | |
pager-active-class | string (css classname) | 'cycle-pager-active' |
The classname to assign to pager links when a particular link references the currently visible slide.
The value of this option should not include a preceding "dot".
|
|
pager-event | string (event name) | 'click.cycle' | The type of event that is bound on the pager links. By default, Cycle2 binds click events. | |
pager-template | • | string | '<span>•</span>' |
A template string which defines how the pager links should be formatted. The template can reference real-time state information from the slideshow as each slide is added.
The default pager link is simply a bullet. Cycle2 uses simple Mustache-style templates by default. |
pause-on-hover | boolean or string | false |
If true an auto-running slideshow will be paused while the mouse is over the slideshow.
You may also specify a jQuery selector string for the value in order to specify the element(s) to which the hover events should be bound.
|
|
paused | boolean | false | If true the slideshow will begin in a paused state. | |
prev | jQuery selector | '> .cycle-prev' | A selector string which identifies the element (or elements) to use as a trigger to advance the slideshow backward. By default, Cycle2 looks for an element with the class .cycle-prev that exists within the slideshow container. | |
prev-event | string (event name) | 'click.cycle' | The type of event that is bound on the prev and next links. By default, Cycle2 binds click events. | |
progressive | selector string | undefined | Identifies an element in the DOM which holds a JSON array representing the slides to be progressively loaded into the slideshow.Example. | |
random | boolean | false | If true the order of the slides will be randomized. This only effects slides that are initially in the markup, not slides added via the addcommand or via Cycle2's image loader functionality. | |
reverse | boolean | false | If true the slideshow will proceed in reverse order and transitions that support this option will run a reverse animation. | |
slide-active-class | string | cycle-slide-active |
The classname to assign to the active slide.
The value of this option should not include a preceding "dot".
|
|
slide-css | • | object hash | {} | An object which defines css properties that should be applied to each slide as it is initialized (once). |
slide-class | string | "cycle-slide" | Name of the class to add to each slide. | |
slides | jQuery selector | '> img' |
A selector string which identifies the elements within the slideshow container that should become slides.
By default, Cycle2 finds all image elements that are immediate children of the slideshow container. |
|
speed | • | integer | 500 | The speed of the transition effect in milliseconds. |
starting-slide | integer | 0 | The zero-based index of the slide that should be initially displayed. | |
swipe | boolean | false | Set to true to enable swipe gesture support for advancing the slideshow forward or back. | |
sync | boolean | true | If true then animation of the incoming and outgoing slides will be synchronized. If false then the animation for the incoming slide will not start until the animation for the outgoing slide completes. | |
timeout | • | integer | 4000 | The time between slide transitions in milliseconds. |
tmpl-regex | • | string | {{((.)?.*?)}} | The default regular expression used for template tokenization. |
update-view | number | 0 |
Determines when the updateView method is invoked (and event is triggered).
If the value is -1 then updateView is only invoked immediately after the slide transition. If the value is 0 then updateView is invoked during the slide transition. If the value is 1 then updateView is invoked immediately upon the beginning of a slide transition and again immediately after the transition. |
Commands
Commands are issued to slideshows by invoking cycle on the container element and passing it a string argument, which is the command name.
Command | Description | Argument | Example Usage |
---|---|---|---|
add | Adds one or more slides to the slideshow. | Slide markup or jQuery object |
var newSlide = '<img src="pic.jpg">'; $('.cycle-slideshow').cycle('add', newSlide); |
destroy | Restores slideshow DOM to it's original state (and unbinds events). | none |
$('.cycle-slideshow').cycle('destroy'); |
goto | Transitions the slideshow to the slide index provided. | zero-based slide index |
// goto 3rd slide $('.cycle-slideshow').cycle('goto', 2); |
next | Transitions the slideshow to the next slide. | none |
$('.cycle-slideshow').cycle('next'); |
pause | Pauses an auto-running slideshow. | none |
$('.cycle-slideshow').cycle('pause'); |
prev | Transitions the slideshow to the previous slide. | none |
$('.cycle-slideshow').cycle('prev'); |
reinit | Reinitializes a slideshow. This is equivalent to issuing the 'destroy' command and then invokingcycle() again. | none |
$('.cycle-slideshow').cycle('reinit'); |
remove | Removes a slide from a running slideshow | zero-based slide index |
// remove 2nd slide $('.cycle-slideshow').cycle('remove', 1); |
resume | Resumes a paused slideshow. | none |
$('.cycle-slideshow').cycle('resume'); |
stop | Stops an auto-running slideshow. | none |
$('.cycle-slideshow').cycle('stop'); |
Declarative Commands
It is possible to issue commands declaratively by using the data-cycle-cmd attribute. You can use this attribute on any element and Cycle2 will use event delegation to issue a cycle command when the element is clicked. For example, to have a random button element pause your slideshow you can do this:
<button data-cycle-cmd="pause">Pause</button>
When the above button is clicked, Cycle2 will auto-generate and run this code for you:
$('.cycle-slideshow').cycle('pause');
To specify a specific slideshow as the target of the command, use the data-cycle-context attribute and set its value to a selector string that identifies the desired slideshow:
<button data-cycle-cmd="pause" data-cycle-context="#mySlideshow">Pause</button>
When the above button is clicked, Cycle2 will auto-generate and run this code for you:
$('#mySlideshow').cycle('pause');
And finally, you can pass an argument to the cycle command using the data-cycle-arg attribute. For example, to remove the first slide in a slideshow:
<button data-cycle-cmd="remove" data-cycle-context="#mySlideshow" data-cycle-arg="0">Remove Slide</button>
Events
Cycle2 emits numerous events as a slideshow runs and you can bind to these events in order to further customize or augment the slideshow's behavior.
All cycle events are triggered on the slideshow element and so can be bound using code like:
$( '#mySlideshow' ).on( 'cycle-eventname', function( event, opts ) { // your event handler code here // argument opts is the slideshow's option hash });
Event Name | Description / Callback Signature |
---|---|
cycle-after |
Triggered after the slideshow has completed transitioning to the next slide.function(event, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag) |
cycle-before |
Triggered just prior to a transition to a new slide.function(event, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag) |
cycle-bootstrap |
This is the first event triggered by a slideshow and it provides an opportunity to override the slideshow options and API methods. The API arg is an object hash of this slideshow instance's API methods.function(event, optionHash, API) |
cycle-destroyed |
Triggered after the slideshow has been destroyed by the 'destroy' command.function(event, optionHash) |
cycle-finished |
Triggered after the slideshow has terminated due to the 'loop' option.function(event, optionHash) |
cycle-initialized |
Triggered after a slideshow has been fully initalized.function(event, optionHash) |
cycle-next |
Triggered after the slideshow has started advancing due to the 'next' command.function(event, optionHash) |
cycle-pager-activated |
Triggered after the slideshow has started transitioning due to a pager link event.function(event, optionHash) |
cycle-paused |
Triggered after the slideshow has been paused as a result of either the 'pause' command or the 'pause-on-hover' option.function(event, optionHash) |
cycle-post-initialize |
Triggered immediately after running the full initialation logic on the slideshow.function(event, optionHash) |
cycle-pre-initialize |
Triggered prior to running the full initialation logic on the slideshow (but after the initial slide set has been added).function(event, optionHash) |
cycle-prev |
Triggered after the slideshow has started advancing due to the 'prev' command.function(event, optionHash) |
cycle-resumed |
Triggered after the slideshow has been paused as a result of either the 'resume' command or the 'pause-on-hover' option.
function(event, optionHash) |
cycle-slide-added |
Triggered after a slide has been added to the slideshowfunction(event, jQueryWrappedSlideEl) |
cycle-slide-removed |
Triggered after a slide has been removed from the slideshow due to the 'remove' command.function(event, indexOfSlideRemoved, removedSlideEl) |
cycle-stopped |
Triggered after the slideshow has been stopped by the 'stop' command.function(event, optionHash) |
cycle-transition-stopped |
Triggered after animation of one or more slides has been interrupted due to manual slide advancement or due to either the 'stop' or 'destroy' command being issued.function(event, optionHash) |
cycle-update-view |
Triggered after initialization, after a slide transition, and after a slide has been added or removed.function(event, optionHash, slideOptionsHash, currentSlideEl) |
You are encouraged to view the source of these demos to understand the power and flexibility the Cycle2 brings to the table.
- Getting Started
- How to get started with simple, basic slideshows.
- Adding Slides
- How to add slides to a running slideshow.
- Auto Height
- How to leverage Cycle2's auto-height support.
- Bookmarkable Slides
- How to leverage Cycle2's data-cycle-hash attribute to have bookmarkable slides.
- Captions
- How to add captions to your slideshow. From simple to advanced.
- Carousel Pager
- Two synchronized slideshows using a carousel pager and prev/next anchors.
- Centered Slides
- How to center slides horizontally and vertically (using Cycle2's center plugin).
- Commands
- How to issue commands to a running slideshow and how to bind arbitary elements to issue those commands (pause, resume, stop, goto, etc).
- Continuous
- How to have a continuously moving slideshow.
- Image Loader
- How to use Cycle2's image loading facilities in order to delay the start of a slideshow until some or all images have been loaded.
- Manual Fx
- How to override the default transition effect when the transition is initiated through user interaction (as apposed to a timer).
- Nested Slideshows
- How to have a slideshow of slideshows (inception!)
- Non-Image Slides
- How to use elements other than images as slideshow slides. This includes how to wrap images in anchors so that they can be links and how to build slides from several elements.
- Overlays
- How to add information overlays to your slideshow.
- Pagers
- How to add pagers to your slideshow. From simple to advanced.
- Pause-On-Hover
- How to pause a slideshow when the mouse moves over it and how to identify other elements to trigger this behavior.
- Prev/Next Controls
- How to use prev/next controls to advance a slideshow forward and back.
- Progress Bar
- How to listen to cycle events in order to display a slideshow progressbar.
- Progressive Image Loading
- How to use Cycle2's progressive image loading facilities to minimize bandwidth and load images as needed.
- Swipe Gesture
- How to use a swipe gesture to trigger slide changes on touch devices.
- Synchronized Slideshows
- How to synchronized multiple slideshows to transition sequentially.
- Template Customization
- How to customize Cycle2's template syntax.
- Transition: Carousel
- Examples of the Carousel transition effect.
- Transition: Flip
- Examples of the CSS3 Flip transition effects.
- Transition: Shuffle
- Examples of the Shuffle transition effect.
- Transition: Tile
- Examples of the Tile transition effects.
- YouTube
- An example of displaying YouTube videos in slideshow.