Flickity can be installed via npm, yarn, or by including a <script>
tag.
npm:
npm install flickity
yarn:
yarn add flickity
<script>
tag: Download the Flickity JavaScript and CSS files from the Flickity website and include them in your HTML file. For example:
<link rel="stylesheet" href="flickity.css">
<script src="flickity.pkgd.min.js"></script>
Remember to replace "flickity.css"
and "flickity.pkgd.min.js"
with the actual paths to your downloaded files. Using the packaged flickity.pkgd.min.js
file includes all the necessary dependencies.
Once installed, you need to include the Flickity CSS and JavaScript in your project. This is done as described in the Installation section above. Then you’ll need to create a container element for your carousel and add the necessary classes and attributes.
The basic HTML structure looks like this:
<div class="carousel" data-flickity='{ "freeScroll": true }'>
<div class="carousel-cell"></div>
<div class="carousel-cell"></div>
<div class="carousel-cell"></div>
</div>
.carousel
: This is the main container element for your Flickity carousel. It must have the data-flickity
attribute (explained below)..carousel-cell
: This class is applied to each item (cell) within the carousel.The data-flickity
attribute is a JSON string containing Flickity options. In this example, freeScroll: true
enables free scrolling. You can customize many aspects of Flickity using these options (see the Flickity documentation for a complete list).
Let’s create a simple carousel with three images. First, include the necessary CSS and JavaScript as in the Installation section. Then, add the following HTML:
<div class="carousel" data-flickity='{ "wrapAround": true }'>
<div class="carousel-cell"><img src="image1.jpg" alt="Image 1"></div>
<div class="carousel-cell"><img src="image2.jpg" alt="Image 2"></div>
<div class="carousel-cell"><img src="image3.jpg" alt="Image 3"></div>
</div>
This code creates a carousel that wraps around (meaning you can seamlessly loop from the last to the first image and vice-versa), thanks to "wrapAround": true
in the data-flickity
attribute. Replace "image1.jpg"
, "image2.jpg"
, and "image3.jpg"
with the actual paths to your images. Remember to adjust the styling as needed to fit your design. You can add more cells to the carousel to display more images. For further customization, refer to the Flickity documentation’s extensive options and API details.
By default, Flickity selects all direct children of the carousel element as cells. However, you can customize this using the cellSelector
option. This is useful if your carousel items aren’t direct children, or if you need more specific selection criteria.
For example, if your carousel cells are within <li>
elements:
<div class="carousel" data-flickity='{ "cellSelector": "li" }'>
<li><img src="image1.jpg" alt="Image 1"></li>
<li><img src="image2.jpg" alt="Image 2"></li>
<li><img src="image3.jpg" alt="Image 3"></li>
</div>
In this case, cellSelector: "li"
tells Flickity to select all <li>
elements within the .carousel
as cells. You can use any valid CSS selector here.
Flickity offers a wide range of options to customize its behavior. These options can be set using the data-flickity
attribute or by passing an options object to the Flickity constructor. Here’s a summary of some key options:
accessibility
: (boolean) Enables/disables ARIA attributes for accessibility. Defaults to true
.adaptiveHeight
: (boolean) Adapts the carousel height to the tallest cell. Defaults to false
.cellAlign
: ('left'
, 'center'
, 'right'
) Aligns cells within the carousel. Defaults to 'center'
.contain
: (boolean) Prevents cells from overflowing the carousel container. Defaults to false
.draggable
: (boolean) Enables/disables dragging. Defaults to true
.freeScroll
: (boolean) Enables free scrolling without snapping to cells. Defaults to false
.friction
: (number) Friction coefficient for momentum. Higher values mean slower deceleration. Defaults to 0.28
.initialIndex
: (number) The index of the initially selected cell. Defaults to 0
.percentPosition
: (boolean) Uses percentage-based positioning instead of pixel-based. Defaults to false
.prevNextButtons
: (boolean) Shows/hides the previous and next buttons. Defaults to true
.pageDots
: (boolean) Shows/hides page dots. Defaults to true
.wrapAround
: (boolean) Enables wrapping around from the last to the first cell and vice-versa. Defaults to false
.autoPlay
: (number or boolean) Enables auto-playing the carousel. A number sets the interval in milliseconds. Defaults to false
.This is not an exhaustive list. Refer to the complete Flickity documentation for all available options and their descriptions.
Flickity can adapt to different screen sizes using responsive settings. You can achieve this using either media queries and the data-flickity
attribute or by directly updating options using JavaScript.
Using Media Queries: You can apply different data-flickity
attributes to your carousel element based on screen size using CSS media queries. For example:
@media (min-width: 768px) {
.carousel {
: "left" }';
data-flickity='{ "cellAlign"
} }
Updating Options with JavaScript: You can listen to window resize events and update Flickity options accordingly. This is more flexible for complex responsive behaviour. For example:
const flkty = new Flickity( '.carousel' );
window.addEventListener('resize', function() {
if (window.innerWidth > 768) {
.update({ cellAlign: 'left' });
flktyelse {
} .update({ cellAlign: 'center' });
flkty
}; })
Data attributes provide a convenient way to configure Flickity options directly in your HTML. You place the options within a JSON object in the data-flickity
attribute of your carousel container. For example:
<div class="carousel" data-flickity='{ "wrapAround": true, "autoPlay": 3000 }'>
<!-- Carousel cells -->
</div>
This sets wrapAround
to true
and enables auto-play with a 3000ms interval. This approach is concise for simple configurations. For more complex scenarios or dynamic changes, using JavaScript to instantiate and configure Flickity is recommended.
Flickity provides several methods to interact with and control the carousel after initialization. These methods are called on the Flickity instance.
.select(index, isInstant)
Selects a specific cell by its index.
index
: (number) The index of the cell to select (0-based).isInstant
: (boolean, optional) If true
, the transition is immediate; otherwise, the default animation is used. Defaults to false
.const flkty = new Flickity( '.carousel' );
.select(2); // Selects the third cell.
flkty.select(0, true); // Selects the first cell instantly. flkty
.previous()
Selects the previous cell.
const flkty = new Flickity( '.carousel' );
.previous(); flkty
.next()
Selects the next cell.
const flkty = new Flickity( '.carousel' );
.next(); flkty
.resize()
Updates the Flickity layout after changes to the carousel’s content or dimensions. This is crucial after dynamically adding or removing cells, or resizing the window.
const flkty = new Flickity( '.carousel' );
.resize(); flkty
.destroy()
Completely removes Flickity from the element, restoring its original state. This is useful for cleanup when the carousel is no longer needed.
const flkty = new Flickity( '.carousel' );
.destroy(); flkty
.reloadCells()
Re-selects the cells from the DOM. Use this if the content of the cells has changed significantly (e.g., the number of cells has changed, or their dimensions have altered unexpectedly).
const flkty = new Flickity( '.carousel' );
.reloadCells(); flkty
.getCellElements()
Returns an array of the DOM elements representing the cells.
const flkty = new Flickity( '.carousel' );
const cells = flkty.getCellElements();
console.log(cells); // Array of cell DOM elements
.getCurrentCell()
Returns the currently selected cell’s element.
const flkty = new Flickity( '.carousel' );
const currentCell = flkty.getCurrentCell();
console.log(currentCell); // DOM element of the current cell
.getCells()
Returns an array of Flickity Cell instances. Each Cell instance contains more detailed information about each cell than just the DOM element (e.g., its index, element, and position).
const flkty = new Flickity( '.carousel' );
const cells = flkty.getCells();
console.log(cells); // Array of Flickity Cell instances
console.log(cells[0].element); // DOM element of the first cell
console.log(cells[0].index); // Index of the first cell
Flickity triggers various events throughout its lifecycle, allowing you to respond to changes in the carousel’s state. These events can be used to enhance the user experience or integrate Flickity with other JavaScript components.
Flickity events are dispatched on the Flickity instance. They are typically handled using the addEventListener
method. Events are categorized into several types:
select
, change
, dragStart
, dragMove
, and dragEnd
.resize
, dragEnd
).ready
(when Flickity is fully initialized).Here’s a list of key Flickity events and their descriptions:
ready
: Fired when Flickity is fully initialized and ready.select
: Fired when a new cell is selected. The event object has a selectedIndex
property indicating the index of the newly selected cell.change
: Fired when the visible cells change (e.g., due to dragging or selecting). The event object has a selectedElement
property.dragStart
: Fired when a drag begins.dragMove
: Fired while dragging is in progress.dragEnd
: Fired when a drag ends.staticClick
: Fired when a click occurs on a cell while the carousel is not being dragged.resize
: Fired when the carousel is resized.destroy
: Fired when the carousel is destroyed using .destroy()
.cellSelect
: Fired when a cell is selected. The event object includes selectedCell
which is the Flickity Cell instance.This example shows how to handle the select
and resize
events:
const flkty = new Flickity( '.carousel' );
.on( 'select', function( event ) {
flktyconsole.log( `Selected cell index: ${event.selectedIndex}` );
// Add your code to handle the selection event here, e.g., update UI elements
;
})
.on( 'resize', function() {
flktyconsole.log( 'Carousel resized' );
// Perform actions needed after the carousel resizes, like adjusting other elements
;
})
//Alternatively, using addEventListener
.addEventListener('select', function(event) {
flktyconsole.log(`Selected cell index: ${event.selectedIndex}`);
;
})
//Removing Event Listeners
.removeEventListener('select', function(event) {
flkty//Do something
;
})
Remember to replace '.carousel'
with the selector for your Flickity carousel element. You can handle other events in a similar manner, using the appropriate event name. The on()
method is a shorthand for addEventListener()
. removeEventListener()
is used to remove event listeners. Consult the Flickity documentation for details on the event objects and properties for each event.
This section covers more complex scenarios and techniques for working with Flickity.
Flickity offers great flexibility in how you structure your carousel cells. While the basic example uses <div>
elements, you can use any HTML element as a cell. This enables you to create complex and visually rich carousels.
For example, you could create cells with custom layouts using flexbox or grid:
<div class="carousel" data-flickity>
<div class="carousel-cell">
<div class="cell-image"><img src="image1.jpg" alt="Image 1"></div>
<div class="cell-caption">Image Caption 1</div>
</div>
<div class="carousel-cell">
<div class="cell-image"><img src="image2.jpg" alt="Image 2"></div>
<div class="cell-caption">Image Caption 2</div>
</div>
<!-- More cells -->
</div>
Remember to style the .carousel-cell
, .cell-image
, and .cell-caption
classes appropriately to achieve the desired layout and appearance.
You can easily have multiple Flickity carousels on a single page. Simply create separate containers for each carousel and initialize Flickity on each:
const carousel1 = new Flickity( '.carousel-1' );
const carousel2 = new Flickity( '.carousel-2', {
cellAlign: 'left' // Different options for each carousel
; })
Each instance will have its own properties and methods, allowing for independent control over each carousel.
Flickity handles dragging and swiping by default, but you can customize the behavior using options like draggable
, friction
, and event listeners (dragStart
, dragMove
, dragEnd
). You could, for example, add visual feedback during dragging or disable dragging altogether:
const flkty = new Flickity('.carousel', {
draggable: true, //Enable/disable dragging
friction: 0.5 // Adjust dragging friction
;
})
.on( 'dragStart', function() {
flkty// Add visual feedback, e.g., highlight dragged cell
;
})
.on('dragMove', function(event) {
flkty//Update a progress bar based on current drag position (event.x, event.y)
; })
To ensure your Flickity carousel is accessible, utilize ARIA attributes. Flickity enables ARIA attributes by default (accessibility: true
). Additionally:
Flickity can be integrated with other JavaScript libraries. For example, you could combine it with libraries for lazy-loading images, lightboxes, or parallax effects. The key is to ensure the other library’s events and methods don’t conflict with Flickity’s. This might involve careful timing of events or using techniques like event delegation. For instance, consider using Flickity’s ready
event to initialize a lazy-loading library after Flickity is fully setup.
Remember to thoroughly test your integration to ensure everything works as expected.
This section helps you diagnose and resolve common problems encountered when using Flickity.
Flickity doesn’t work: Ensure you’ve correctly included the Flickity CSS and JavaScript files in your HTML. Check your browser’s developer console for JavaScript errors. Verify that your carousel container has the correct class (carousel
) and data-flickity
attribute.
Cells aren’t displaying correctly: Inspect the CSS applied to your carousel and cells. Incorrect dimensions or conflicting styles can cause layout problems. Check for typos in your class names. Ensure that cellSelector
is correctly configured if cells aren’t direct children of the carousel container.
Dragging is unresponsive: Check if draggable
is set to true
in your options. Make sure there are no conflicting JavaScript libraries interfering with Flickity’s drag events. Try disabling other JavaScript to isolate the problem.
Autoplay doesn’t work: Verify that autoPlay
is set correctly (to a number representing the milliseconds between slides). Ensure there’s no JavaScript code halting or conflicting with the automatic transitions.
Flickity isn’t responding to window resizing: Make sure you’re calling flkty.resize()
after the window resize event or that your responsive settings (using media queries or JavaScript) are working correctly.
Cells are overlapping: Check for styling conflicts that might be causing the cells to overlap. Make sure the contain
option is set appropriately if you intend to keep cells from overflowing.
Use your browser’s developer tools: The browser’s developer console provides invaluable information about errors, warnings, and the state of your web page. Use the console to debug JavaScript issues and examine the DOM structure of your Flickity carousel.
Inspect the Flickity instance: Use your browser’s debugger to step through the Flickity JavaScript code and examine the values of variables. This allows you to see the state of Flickity at various points in its execution.
Simplify your code: To isolate the source of a problem, temporarily remove or comment out sections of your code to see if the issue persists. Start with a minimal working example and gradually reintroduce elements to identify the problematic part.
Check Flickity’s documentation: The official Flickity documentation is your primary resource. Search for error messages, unusual behavior, or specific configuration options.
Minimize the number of cells: Fewer cells lead to better performance, especially on low-powered devices. Consider loading cells dynamically as the user scrolls instead of loading everything at once.
Optimize images: Use appropriately sized and optimized images. Large images can slow down page load and Flickity’s performance. Consider lazy-loading for images outside the viewport.
Use efficient CSS: Avoid complex or computationally expensive CSS styles that could impact rendering performance. Keep your CSS concise and well-organized.
Use a build process: For larger projects, a build process can optimize your code (minification, bundling) resulting in smaller file sizes and faster loading times.
This section provides a detailed reference for the Flickity API.
The Flickity
class is the main constructor for creating a Flickity carousel.
Constructor:
new Flickity( element, options )
element
: (HTMLElement or string selector) The DOM element to turn into a Flickity carousel.options
: (object, optional) An object containing Flickity options (see Flickity Options section below).After creating a Flickity instance, you can use the following methods to interact with it:
.select(index, isInstant)
: Selects a cell by its index. isInstant
(boolean, optional) determines whether the transition is immediate..previous()
: Selects the previous cell..next()
: Selects the next cell..resize()
: Updates the Flickity layout. Call this after adding, removing, or modifying cells..destroy()
: Removes Flickity from the element..reloadCells()
: Reselects cells from the DOM. Use after significant content changes..getCellElements()
: Returns an array of the cell DOM elements..getCurrentCell()
: Returns the currently selected cell’s DOM element..getCells()
: Returns an array of Flickity Cell instances (containing more detailed cell information)..on(eventName, listener)
: Adds an event listener..once(eventName, listener)
: Adds an event listener that is removed after the first event..off(eventName, listener)
: Removes an event listener.Event Handling Methods:
The on()
, once()
, and off()
methods are used to manage events. The eventName
is a string representing the event name (e.g., select
, resize
, change
). The listener
is a callback function that will be executed when the event is triggered. For example:
.on( 'select', function( event ) {
flktyconsole.log( 'Selected cell:', event.selectedIndex );
; })
These options can be passed to the Flickity
constructor or set using the data-flickity
attribute:
accessibility
: (boolean) Enables/disables ARIA attributes. Defaults to true
.adaptiveHeight
: (boolean) Adapts the carousel height to the tallest cell. Defaults to false
.cellAlign
: ('left'
, 'center'
, 'right'
) Aligns cells within the carousel. Defaults to 'center'
.cellSelector
: (string) CSS selector for cells. Defaults to '> *
(all direct children).contain
: (boolean) Prevents cells from overflowing the container. Defaults to false
.draggable
: (boolean) Enables/disables dragging. Defaults to true
.friction
: (number) Friction coefficient for momentum. Defaults to 0.28
.freeScroll
: (boolean) Enables free scrolling. Defaults to false
.initialIndex
: (number) Index of the initially selected cell. Defaults to 0
.percentPosition
: (boolean) Uses percentage-based positioning. Defaults to false
.prevNextButtons
: (boolean) Shows/hides previous/next buttons. Defaults to true
.pageDots
: (boolean) Shows/hides page dots. Defaults to true
.wrapAround
: (boolean) Enables wrapping around. Defaults to false
.autoPlay
: (number or boolean) Enables auto-playing. A number sets the interval in milliseconds. Defaults to false
.imagesLoaded
: (boolean) Waits for images to load before initializing. Defaults to true
.This is not an exhaustive list. Refer to the complete Flickity documentation for all available options and their descriptions. The default values are shown above, but remember that the latest documentation might have updates.