Slick Carousel

repository·master·Indexed 12 days ago

https://github.com/kenwheeler/slick

A highly flexible, responsive carousel plugin for jQuery supporting touch swiping, infinite looping, and responsive breakpoints. Version 2.0.0 features include data-attribute settings, a comprehensive API for slide manipulation (slickAdd, slickRemove, slickGoTo), and customizable Sass variables for styling.

Tokens
4.4K
Snippets
24
Records
27
Agent score
49%

What's inside Slick

  1. Use Responsive Breakpoints in Slick

    master

    The responsive option allows you to change settings at specific breakpoints. You can also use the value "unslick" to destroy the carousel at a specific breakpoint.

    $(".slider").slick({
      infinite: false,
      responsive: [{
          breakpoint: 1024,
          settings: {
            slidesToShow: 3,
            infinite: true
          }
        }, {
          breakpoint: 600,
          settings: {
            slidesToShow: 2,
            dots: true
          }
        }, {
          breakpoint: 300,
          settings: "unslick" // destroys slick
        }]
    });
  2. Install Slick Carousel via CDN

    master

    To use Slick immediately without a package manager, include the CSS and JS files from a CDN.

    For jQuery 4.0 or higher: Use the @master branch links for the latest version.

    For jQuery 3.0 or lower: Use the versioned links (e.g., @1.8.1).

    <!-- For jQuery 4.0+ -->
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/kenwheeler/slick@master/slick/slick.css"/>
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/kenwheeler/slick@master/slick/slick-theme.css"/>
    
    <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/kenwheeler/slick@master/slick.min.js"></script>
    
    <!-- For jQuery 3.0 or lower -->
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick.css"/>
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick-theme.css"/>
    
    <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick.min.js"></script>
  3. Install Slick Carousel via NPM or Bower

    master

    You can install Slick using standard package managers.

    Note: Version 2.0.0 is currently not available via Bower or NPM; use CDN links for the latest version.

    # Bower
    bower install --save slick-carousel
    
    # NPM
    npm install slick-carousel
  4. How Slick events work

    master

    Slick triggers several events that you can listen to using jQuery's .on() method. Common events include:

    • init: Fired when the carousel is initialized.
    • beforeChange: Fired before the slide transition begins. Arguments: [instance, currentSlide, nextSlide].
    • afterChange: Fired after the slide transition completes. Arguments: [instance, index].
    • lazyLoaded: Fired when an image has finished loading via lazy loading. Arguments: [instance, image, imageSource].
    • lazyLoadError: Fired when an image fails to load. Arguments: [instance, image, imageSource].
    • lazyLoadError: Fired when an image fails to load. Arguments: [instance, image, imageSource].
    • reInit: Fired when the carousel is reinitialized.
    • setPosition: Fired when the carousel position is updated.
    $('.your-slider').on('afterChange', function(event, slick, currentSlide) {
      console.log('Slide changed to: ' + currentSlide);
    });
  5. Customize Slick with Sass Variables

    master

    If using Sass, you can customize the appearance of Slick using the following variables:

    • $slick-font-path: Directory path for the slick icon font.
    • $slick-font-family: Font-family for slick icon font.
    • $slick-arrow-color: Color of the left/right arrow icons.
    • $slick-dot-color: Color of the navigation dots.
    • $slick-dot-color-active: Color of the active navigation dot.
    • $slick-prev-character: Unicode character code for the previous arrow icon.
    • $slick-next-character: Unicode character code for the next arrow icon.
  6. Initialize Slick carousel with jQuery

    master

    To initialize a Slick carousel, call the .slick() method on a jQuery object. You can pass an optional configuration object to customize the behavior. Slick can also be configured via HTML data-slick attributes on the element.

    $('.your-element').slick({
      // settings go here
      infinite: true,
      slidesToShow: 3,
      slidesToScroll: 1
    });
  7. Initialize Slick using Data Attribute Settings

    master

    In Slick 1.5+, you can define settings directly in your HTML using the data-slick attribute. You must still call $(element).slick() to initialize the carousel.

    <div data-slick='{"slidesToShow": 4, "slidesToScroll": 4}'>
      <div><h3>1</h3></div>
      <div><h3>2</h3></div>
      <div><h3>3</h3></div>
      <div><h3>4</h3></div>
      <div><h3>5</h3></div>
      <div><h3>6</h3></div>
    </div>
    
    <script>
      // You still need to call this to initialize
      $('.your-element').slick();
    </script>
  8. Handle Slick Carousel Events

    master

    Slick uses jQuery events for callbacks. To ensure events are captured, define them before initializing the slider.

    Common events include:

    • afterChange: After slide change.
    • beforeChange: Before slide change.
    • init: When Slick initializes (must be defined before initialization).
    • swipe: Fires after swipe/drag.
    • edge: Fires when an edge is overscrolled in non-infinite mode.
    // On swipe event
    $('.your-element').on('swipe', function(event, slick, direction){
      console.log(direction);
    });
    
    // On before slide change
    $('.your-element').on('beforeChange', function(event, slick, currentSlide, nextSlide){
      console.log(nextSlide);
    });
  9. Call Slick Methods

    master

    You can interact with a Slick instance by calling methods through the jQuery .slick() function.

    Core Methods:

    • slick(options): Initializes Slick.
    • unslick: Destroys Slick.
    • slickNext: Triggers next slide.
    • slickPrev: Triggers previous slide.
    • slickGoTo(index, dontAnimate): Goes to slide by index.
    • slickCurrentSlide: Returns the current slide index.
    • slickAdd(element, index, addBefore): Adds a slide.
    • slickRemove(index, removeBefore): Removes a slide.
    • slickSetOption(option, value, refresh): Changes a single option.
    // Add a slide
    $('.your-element').slick('slickAdd',"<div></div>");
    
    // Get the current slide
    var currentSlide = $('.your-element').slick('slickCurrentSlide');
    
    // Change the speed
    $('.your-element').slick('slickSetOption', 'speed', 5000, true);
    
    // Destroy with
    $('.your-element').slick('unslick');
  10. Slick carousel configuration options

    master

    Slick provides a wide range of settings to control the carousel behavior. Below are the available default options:

    OptionDefault
    accessibilitytrue
    adaptiveHeightfalse
    appendArrows$(element)
    appendDots$(element)
    arrowstrue
    asNavFornull
    prevArrow'<button class="slick-prev" aria-label="Previous" type="button">Previous</button>'
    nextArrow'<button class="slick-next" aria-label="Next" type="button">Next</button>'
    autoplayfalse
    autoplaySpeed3000
    centerModefalse
    centerPadding'50px'
    cssEase'ease'
    customPagingfunction(slider, i) { return $('<button type="button"></button>').text(i + 1); }
    dotsfalse
    dotsClass'slick-dots'
    draggabletrue
    easing'linear'
    edgeFriction0.35
    fadefalse
    focusOnSelectfalse
    focusOnChangefalse
    infinitetrue
    initialSlide0
    lazyLoad'ondemand'
    mobileFirstfalse
    pauseOnHovertrue
    pauseOnFocustrue
    pauseOnDotsHoverfalse
    respondTo'window'
    responsivenull
    rows1
    rtlfalse
    slide''
    slidesPerRow1
    slidesToShow1
    slidesToScroll1
    speed500
    swipetrue
    swipeToSlidefalse
    touchMovetrue
    touchThreshold5
    useCSStrue
    useTransformtrue
    variableWidthfalse
    verticalfalse
    verticalSwipingfalse
    waitForAnimatetrue
    zIndex1000
  11. Configure Slick Carousel Settings

    master

    Slick accepts an options object during initialization. Common settings include:

    • accessibility (boolean): Enables tabbing and arrow key navigation.
    • autoplay (boolean): Enables auto play of slides.
    • dots (boolean): Shows current slide indicator dots.
    • infinite (boolean): Enables infinite looping.
    • slidesToShow (int): Number of slides to show at a time.
    • slidesToScroll (int): Number of slides to scroll at a time.
    • speed (int): Transition speed in ms.
    • swipe (boolean): Enables touch swipe.

    See the full list of options in the documentation for advanced configurations like lazyLoad, centerMode, and variableWidth.

  12. Navigate to a specific slide with slickGoTo()

    master

    Use slickGoTo(slide, dontAnimate) to jump to a specific slide index.

    • slide: The index of the slide to navigate to.
    • dontAnimate: (Optional) A boolean indicating whether to skip the animation.
    // Go to the third slide (index 2)
    $('.your-slider').slick('slickGoTo', 2);
    
    // Go to the third slide without animation
    $('.your-slider').slick('slickGoTo', 2, true);