Professional Documents
Culture Documents
J Carousel
J Carousel
J Carousel
Release 0.3.0-beta
Jan Sorgalla
CONTENTS
Reference 1.1 Installation . 1.2 Conguration 1.3 API . . . . . 1.4 Usage . . . . 1.5 Events . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
3 3 4 7 9 12 17 17 29 29 31 33
Plugins 2.1 Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Cookbook 3.1 How to dene a custom start position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Credits License
4 5
ii
jCarousel is a jQuery plugin for controlling a list of items in horizontal or vertical order. It provides a full-featured and exible toolset for navigating any HTML based content in a carousel-like fashion. Download | Repository at GitHub | Bugtracker
CONTENTS
CONTENTS
CHAPTER
ONE
REFERENCE
1.1 Installation
To use the jCarousel component, include the jQuery library and the jCarousel source le into your HTML document:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script> <script type="text/javascript" src="/path/to/jquery.jcarousel.js"> </script>
jCarousel expects a very basic HTML markup structure inside your HTML document:
<div class="jcarousel"> <ul> <li>...</li> <li>...</li> </ul> </div>
Note: The documentation refers to the elements as root element, list element and item element(s):
<div class="jcarousel"> <--------------------------------| Root element <ul> <-------------------------------| List element | <li>...</li> <---| Item element | | <li>...</li> <---| Item element | | </ul> <------------------------------| | </div> <-------------------------------------------------|
This structure is only an example and not required. You could also use a structure like:
<div class="mycarousel"> <-------------------------------| Root element <div> <------------------------------| List element | <p>...</p> <-----| Item element | | <p>...</p> <-----| Item element | | </div> <-----------------------------| | </div> <-------------------------------------------------|
The only requirement is, that it consists of a root element, list element and item elements.
1.1.1 Setup
To setup jCarousel, add the following code to your HTML document:
<script type="text/javascript"> $(function() { $(.jcarousel).jcarousel({ // Configuration goes here }); }); </script>
See Conguration for all available conguration options. These are the minimal CSS settings for a horizontal carousel:
.jcarousel { position: relative; overflow: hidden; /* You need at least a height, adjust this to your needs */ height: 100px; } .jcarousel ul { width: 20000em; position: absolute; list-style: none; margin: 0; padding: 0; } .jcarousel li { float: left; }
1.2 Conguration
jCarousel accepts the following options: See Also: Setup Set conguration options on initialization. reload method Set conguration options at runtime. list A function or a jQuery selector to select the list inside the root element. A function will be called in the context of the carousel instance. Example:
$(.jcarousel).jcarousel({ list: .jcarousel-list });
Default:
function() { return this.element().children().eq(0); }
Chapter 1. Reference
items A function or a jQuery selector to select the items inside the list element. A function will be called in the context of the carousel instance. Example:
$(.jcarousel).jcarousel({ items: .jcarousel-item });
Default:
function() { return this.list().children(); }
animation The speed of the scroll animation as string in jQuery terms ("slow" or "fast") or milliseconds as integer (See the documentation for jQuery animate). Alternatively, this can be a map of options like the one jQuery.animate accepts as second argument. Example:
$(.jcarousel).jcarousel({ animation: slow }); $(.jcarousel).jcarousel({ animation: { duration: 800, easing: linear, complete: function() { } } });
Default: 400 transitions If set to true, CSS3 transitions are used for animations. Alternatively, this can be a map of the following options: transforms: If set to true, 2D transforms are used for better hardware acceleration. transforms3d: If set to true, 3D transforms are used for full hardware acceleration. easing: Value will be used as the transition-timing-function (e.g. ease or linear). Warning: jCarousel does not check if the users browser supports transitions and/or transforms. You have to do that yourself when setting the option. You can for example use Modernizr for browser feature detection. If youre not including it already in your site, you can use this minimal build. Example:
$(.jcarousel).jcarousel({ transitions: true }); $(.jcarousel).jcarousel({ transitions: Modernizr.csstransitions ? { transforms: Modernizr.csstransforms,
1.2. Conguration
Default: false wrap Species whether to wrap at the rst or last item (or both) and jump back to the start/end. Options are "first", "last", "both" or "circular" as string. If set to null, wrapping is turned off (default). Example:
$(.jcarousel).jcarousel({ wrap: both });
Default: null vertical Species whether the carousel appears in vertical orientation. Changes the carousel from a left/right style to a up/down style carousel. If set to null, jCarousel tries to auto-detect the orientation by simply checking if the lists height is greater than the lists width. Example:
$(.jcarousel).jcarousel({ vertical: true });
Default: null rtl Species wether the carousel appears in RTL (Right-To-Left) mode. If set to null, jCarousel looks for dir="rtl" attribute on the root element (or to any of its parent elements) and if found, automatically sets rtl to true. Example:
$(.jcarousel).jcarousel({ rtl: true });
Default: null center Species wether the targeted item should be centered inside the root element. Warning: This feature is experimental and may not work with all setups.
Chapter 1. Reference
Example:
$(.jcarousel).jcarousel({ center: true });
Default: false
1.3 API
See Also: Calling methods on the jCarousel instance Call methods on the jCarousel instance.
.jcarousel(reload [, options]) Reloads the carousel. This method is useful to reinitialize the carousel if you have changed the content of the list from the outside or want to change options that affect appearance of the carousel at runtime. Example:
$(.jcarousel).jcarousel(reload, { animation: slow });
.jcarousel(destroy) Removes the jCarousel functionality completely. This will return the element back to its initial state. Example:
1.3. API
$(.jcarousel).jcarousel(destroy);
Chapter 1. Reference
1.4 Usage
1.4.1 Calling methods on the jCarousel instance
If you have created a carousel like:
$(function() { $(.jcarousel).jcarousel(); });
The rst argument is the method name. The following arguments are the arguments for the called method. See API for available methods.
Available formats for the target argument index Scrolls to the item at the given index (Note that indexes are 0-based). Example:
$(.jcarousel).jcarousel(scroll, 0);
-index Scrolls to the item at the given index counting backwards from the end of the list. Example:
$(.jcarousel).jcarousel(scroll, -1);
+=offset Scrolls the carousel forward by the given offset relatively from the current position. Example:
$(.jcarousel).jcarousel(scroll, +=1);
1.4. Usage
-=offset Scrolls the carousel backwards by the given offset relatively from the current position. Example:
$(.jcarousel).jcarousel(scroll, -=1);
Flexible carousel, the number of visible items depend on the width of the roots parent element:
.jcarousel { position: relative; overflow: hidden; width: 100%; } .jcarousel li { float: left; width: 100px; }
10
Chapter 1. Reference
If auto-detection doesnt work, you can explicitly pass the rtl option (the dir="rtl" attribute is still required):
$(.jcarousel).jcarousel({ rtl: true });
Note: When running a carousel in RTL mode, you should ensure to oat the items to the right:
.jcarousel[dir=rtl] li { float: right; }
1.4. Usage
11
If you are removing items, make sure they are currently not visible:
$(function() { var carousel = $(.jcarousel), item = carousel.find(li:eq(0)); if (carousel.jcarousel(visible).index(item) < 0) { item.remove(); carousel.jcarousel(reload); } });
1.5 Events
After initialization, jCarousel triggers the following events on the root and the items elements of the carousel: Note: Some events like create.jcarousel are triggered from the constructor, so you have to bind the events before you initialize the carousel:
$(.jcarousel) // Bind first .on(create.jcarousel, function(event, carousel) { // Do something }) // Initialize at last step .jcarousel();
12
Chapter 1. Reference
scrollend.jcarousel Triggered after the scroll method is called. Note: This method is triggered at the end of the scroll method and not when the animation is nished. Example:
$(.jcarousel).on(scrollend.jcarousel, function(event, carousel) { // "this" refers to the root element // "carousel" is the jCarousel instance });
1.5. Events
13
$(.jcarousel).on(animate.jcarousel, function(event, carousel) { // "this" refers to the root element // "carousel" is the jCarousel instance });
itemtargetout.jcarousel Triggered when the item is no longer the targeted item. Example:
$(.jcarousel).on(itemtargetout.jcarousel, li, function(event, carousel) { // "this" refers to the item element // "carousel" is the jCarousel instance });
itemfirstin.jcarousel Triggered when the item becomes the rst visible item. Example:
$(.jcarousel).on(itemfirstin.jcarousel, li, function(event, carousel) { // "this" refers to the item element // "carousel" is the jCarousel instance });
itemfirstout.jcarousel Triggered when the item is no longer the rst visible item. Example:
$(.jcarousel).on(itemfirstout.jcarousel, li, function(event, carousel) { // "this" refers to the item element // "carousel" is the jCarousel instance });
itemlastin.jcarousel Triggered when the item becomes the last visible item. Example:
$(.jcarousel).on(itemlastin.jcarousel, li, function(event, carousel) { // "this" refers to the item element // "carousel" is the jCarousel instance });
14
Chapter 1. Reference
itemlastout.jcarousel Triggered when the item is no longer the last visible item. Example:
$(.jcarousel).on(itemlastout.jcarousel, li, function(event, carousel) { // "this" refers to the item element // "carousel" is the jCarousel instance });
itemfullyvisiblein.jcarousel Triggered when the item becomes a fully visible item. Example:
$(.jcarousel).on(itemfullyvisiblein.jcarousel, li, function(event, carousel) { // "this" refers to the item element // "carousel" is the jCarousel instance });
itemfullyvisibleout.jcarousel Triggered when the item is no longer a fully visible item. Example:
$(.jcarousel).on(itemfullyvisibleout.jcarousel, li, function(event, carousel) { // "this" refers to the item element // "carousel" is the jCarousel instance });
1.5. Events
15
16
Chapter 1. Reference
CHAPTER
TWO
PLUGINS
2.1 Plugins
By default, the jCarousel core only contains the minimum functionality needed to built carousel widgets. Specialized features are provided through plugins. jCarousel comes with the following plugins:
A control is basically a HTML element (<a>, <button> etc.) which scrolls the carousel when clicking on it. A simple basic HTML markup structure would be:
<!-- Wrapper --> <div> <!-- Carousel --> <div class="jcarousel"> <ul> <li>...</li> <li>...</li> </ul> </div> <!-- Controls --> <a class="jcarousel-prev" href="#">Prev</a> <a class="jcarousel-next" href="#">Next</a> </div>
To setup the plugin, add the following code to your HTML document:
<script type="text/javascript"> $(function() { $(.jcarousel).jcarousel({ // Core configuration goes here
17
}); $(.jcarousel-prev).jcarouselControl({ target: -=1 }); $(.jcarousel-next).jcarouselControl({ target: +=1 }); }); </script>
See section Available formats for the target argument for more information about the target argument As you can see, you setup the controls independently from the carousel and the plugin tries to autodetect the carousel. This works best if you enclose the carousel and its controls inside a mutual wrapper element. If that fails or isnt possible, you can pass the related carousel instance as an option:
<script type="text/javascript"> var carousel = $(.jcarousel).jcarousel({ // Core configuration goes here }); $(.jcarousel-prev).jcarouselControl({ target: -=1, carousel: carousel }); </script>
Conguration
The plugin accepts the following options: target The target for the control. Accepts the same argument as the scroll() method. Example:
$(.jcarousel-control).jcarouselControl({ target: +=3 });
Default: +=1 event The event which triggers the control. Example:
$(.jcarousel-control).jcarouselControl({ event: mouseover });
Default: click method The method to call on the carousel. Plugins may provide alternate methods for scrolling the carousel, e.g. ScrollIntoView Plugin. scrollIntoView from the
Alternatively, method can be a function which will be called in the context of the plugin instance.
18
Chapter 2. Plugins
Example:
$(.jcarousel-control).jcarouselControl({ method: scrollIntoView }); $(.jcarousel-control).jcarouselControl({ method: function() { this.carousel() .jcarousel(myScrollingMethod, this.options(target)); } });
Default: scroll carousel The corresponding carousel as jQuery object. This is optional. By default, the plugin tries to autodetect the carousel. Example:
$(.jcarousel-control).jcarouselControl({ carousel: $(.jcarousel) });
Default: null
API
The plugin exposes the following methods: .jcarouselControl(reload [, options]) Reloads the plugin. This method is useful to reinitialize the plugin or if you want to change options at runtime. Example:
$(.jcarousel-control).jcarouselControl(reload, { interval: 1500 });
.jcarouselControl(destroy) Removes the control functionality completely. This will return the element back to its initial state. Example:
$(.jcarousel-control).jcarouselControl(destroy);
Events
After initialization, the plugin triggers the following events on the element: Note: This is how the plugin understands active and inactive states: If the target option is relative, +=1 for example, the control is active if there is at least on more item to scroll, inactive otherwise (if youre at the last item in this case). If the target option is absolute, 0 for example (always scrolls to the rst item), the control is active if the targeted item is at position 0, inactive otherwise.
2.1. Plugins
19
20
Chapter 2. Plugins
The plugin provides a pagination widget for carousels. A simple basic HTML markup structure would be:
<!-- Wrapper --> <div> <!-- Carousel --> <div class="jcarousel"> <ul> <li>...</li> <li>...</li> </ul> </div> <!-- Pagination --> <div class="jcarousel-pagination"> <!-- Pagination items will be generated in here --> </div> </div>
To setup the plugin, add the following code to your HTML document:
<script type="text/javascript"> $(function() { $(.jcarousel).jcarousel({ // Core configuration goes here }); $(.jcarousel-pagination).jcarouselPagination({ // Configuration options }); }); </script>
As you can see, you setup the pagination independently from the carousel and the plugin tries to autodetect the carousel. This works best if you enclose the carousel and its pagination inside a mutual wrapper element. If that fails or isnt possible, you can pass the related carousel instance as an option:
<script type="text/javascript"> var carousel = $(.jcarousel).jcarousel({ // Core configuration goes here }); $(.jcarousel-pagination).jcarouselPagination({ carousel: carousel }); </script>
2.1. Plugins
21
Conguration
The plugin accepts the following options: perPage The number of carousel items per page or a function returning the number. If perPage is not set or null, the plugin calculates the pages depending on the number of visible carousel items. Example:
$(.jcarousel-pagination).jcarouselPagination({ perPage: 3 });
Default: null item A function returning the markup for a page item of the pagination either as string or jQuery object. The function will be called in the context of the plugin instance and receives two arguments: 1. The page number. 2. A jQuery object containing the carousel items visible on this page. Example:
$(.jcarousel-pagination).jcarouselPagination({ item: function(page, carouselItems) { return <li><a href="# + page + ">Page + page + </a></li>; } });
Default:
function(page, carouselItems) { return <a href="# + page + "> + page + </a>; }
carousel The corresponding carousel as jQuery object. This is optional. By default, the plugin tries to autodetect the carousel. Example:
$(.jcarousel-pagination).jcarouselPagination({ carousel: $(.jcarousel) });
Default: null
API
The plugin exposes the following methods: .jcarouselPagination(reload [, options]) Reloads the plugin. This method is useful to reinitialize the plugin or if you want to change options at runtime. Example:
$(.jcarousel-pagination).jcarouselPagination(reload, { interval: 1500 });
22
Chapter 2. Plugins
.jcarouselPagination(destroy) Removes the pagination functionality completely. This will return the element back to its initial state. Example:
$(.jcarousel-pagination).jcarouselPagination(destroy);
Events
After initialization, the plugin triggers the following events on the root and the item elements: Root element events create.jcarouselpagination Triggered on creation of the plugin. Example:
$(.jcarousel-pagination).on(create.jcarouselpagination, function() { // Do something });
2.1. Plugins
23
Item element events active.jcarouselpagination Triggered when the item becomes active. Example:
$(.jcarousel-pagination).on(active.jcarouselpagination, a, function() { // Do something });
You should use delegated events to attach event handler to the pagination items since they are created and removed on the y depending on your conguration. Example:
$(.jcarousel-pagination) .on(active.jcarouselpagination, a, function() { $(this).addClass(active); }) .on(inactive.jcarouselpagination, a, function() { $(this).removeClass(active); });
The plugin provides autoscrolling support for carousels. To setup the plugin, add the following code to your HTML document:
<script type="text/javascript"> $(function() { $(.jcarousel) .jcarousel({ // Core configuration goes here }) .jcarouselAutoscroll({ // Plugin configuration goes here }); }); </script>
24
Chapter 2. Plugins
Conguration
The plugin accepts the following options: target The target for the autoscrolling. Accepts the same argument as the scroll() method. Example:
$(.jcarousel).jcarouselAutoscroll({ target: +=3 });
Default: true
API
The plugin exposes the following methods: .jcarouselAutoscroll(start) Starts the autoscrolling. Example:
$(.jcarousel).jcarouselAutoscroll(start);
.jcarouselAutoscroll(reload [, options]) Reloads the plugin. This method is useful to reinitialize the plugin or if you want to change options at runtime. Example:
$(.jcarousel).jcarouselAutoscroll(reload, { interval: 1500 });
.jcarouselAutoscroll(destroy) Removes the autoscrolling functionality completely. This will return the element back to its initial state.
2.1. Plugins
25
Example:
$(.jcarousel).jcarouselAutoscroll(destroy);
Events
After initialization, the plugin triggers the following events on the element: create.jcarouselautoscroll Triggered on creation of the plugin. Example:
$(.jcarousel).on(create.jcarouselautoscroll, function() { // Do something });
API The plugin exposes the following method: .jcarousel(scrollIntoView, target [, animate [, callback]]) The arguments are similar to the scroll() method. If the targeted item is currently not fully visible, it will be the rst or last fully visible item after calling the method. Example:
$(.jcarousel).jcarousel(scrollIntoView, 2, true, function(scrolled) { if (scrolled) { console.log(The carousel has been scrolled); } else { console.log(The carousel has not been scrolled); } });
2.1. Plugins
27
28
Chapter 2. Plugins
CHAPTER
THREE
COOKBOOK
3.1 How to dene a custom start position
Sometimes, you dont want to start your carousel at the rst item but dene a custom start position. This can be achieved in two ways: Either by listening to the createend event or by dening the start position via CSS.
Assuming your items have a width of 75px, the start item will be the third item.
29
30
Chapter 3. Cookbook
CHAPTER
FOUR
CREDITS
jCarousel is written on top of jQuery and was originally inspired by the Carousel Component by Bill Scott.
31
32
Chapter 4. Credits
CHAPTER
FIVE
LICENSE
Copyright 2013 Jan Sorgalla. Released under the MIT license.
33