jQuery Card Slider with Background Image on Hover - C-Slider

File Size: 86.9 KB
Views Total: 0
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
jQuery Card Slider with Background Image on Hover - C-Slider

C-Slider is a responsive jQuery card carousel for featured content. On tablets and desktops, hovering over a card or focusing its link reveals its summary and fades its image into the background of the entire carousel.

Features

  • Adjustable background crossfade duration.
  • Previous and next arrow navigation with optional forward looping.
  • Optional horizontal touch swipes via Hammer.js.
  • Responsive card widths and individual card backgrounds on phones.
  • Automatic track navigation to cards that receive keyboard focus.
  • Reduced-motion support for track, summary, and background animations.
  • Independent slider instances on a single page.
  • Custom desktop card count and slider height through CSS variables.

How to use it:

1. Load the full jQuery build before the plugin script.

<link rel="stylesheet" href="src/c-slider.css">
<script src="/path/to/cdn/jquery.min.js"></script>
<script src="src/c-slider.js"></script>

2. Create a three-card carousel that displays two cards at a time on desktop. Add your photos to images/ and replace the card links with your own URLs. The data-c-slider attribute initializes the carousel on DOM ready.

<section id="featured-destinations" class="c-slider" data-c-slider
         style="--c-slider-visible: 2; --c-slider-height: 60vh;"
         aria-labelledby="destinations-title">
  <div class="c-slider__header">
    <h2 class="c-slider__title" id="destinations-title">Explore the outdoors</h2>
  </div>

  <button type="button" class="c-slider__arrow c-slider__arrow--prev"
          aria-label="Previous destinations">
    <span class="c-slider__arrow-line" aria-hidden="true"></span>
  </button>
  <button type="button" class="c-slider__arrow c-slider__arrow--next"
          aria-label="Next destinations">
    <span class="c-slider__arrow-line" aria-hidden="true"></span>
  </button>

  <div class="c-slider__background" aria-hidden="true"></div>

  <ul class="c-slider__track">
    <li class="c-slider__slide">
      <a class="c-slider__link" href="/mountains/">
        <div class="c-slider__image"
             style="background-image: url('images/mountains.jpg');"></div>
        <div class="c-slider__content">
          <h3 class="c-slider__slide-title">Mountain trails</h3>
          <p class="c-slider__teaser">Find your next hike.</p>
          <div class="c-slider__summary">
            <p>Explore alpine lakes and ridge walks.</p>
            <span class="c-slider__more">View destination
              <span class="c-slider__more-arrow" aria-hidden="true"></span>
            </span>
          </div>
        </div>
      </a>
    </li>
    <li class="c-slider__slide">
      <a class="c-slider__link" href="/coast/">
        <div class="c-slider__image"
             style="background-image: url('images/coast.jpg');"></div>
        <div class="c-slider__content">
          <h3 class="c-slider__slide-title">Coastal escapes</h3>
          <p class="c-slider__teaser">Follow the shoreline.</p>
          <div class="c-slider__summary">
            <p>Plan a weekend around beaches and quiet coves.</p>
            <span class="c-slider__more">View destination
              <span class="c-slider__more-arrow" aria-hidden="true"></span>
            </span>
          </div>
        </div>
      </a>
    </li>
    <li class="c-slider__slide">
      <a class="c-slider__link" href="/forest/">
        <div class="c-slider__image"
             style="background-image: url('images/forest.jpg');"></div>
        <div class="c-slider__content">
          <h3 class="c-slider__slide-title">Forest retreats</h3>
          <p class="c-slider__teaser">Spend a day among the trees.</p>
          <div class="c-slider__summary">
            <p>Find woodland walks and secluded campsites.</p>
            <span class="c-slider__more">View destination
              <span class="c-slider__more-arrow" aria-hidden="true"></span>
            </span>
          </div>
        </div>
      </a>
    </li>
  </ul>
</section>

Enable touch swipe navigation

Load Hammer.js between jQuery and C-Slider to enable horizontal touch swipes. 

<script src="/path/to/cdn/jquery.min.js"></script>
<script src="/path/to/cdn/hammer.min.js"></script>
<script src="src/c-slider.js"></script>

Initialize the slider with custom options

Remove data-c-slider from the example's <section>. Add this script after c-slider.js to disable forward looping and set a 500ms background fade:

$(function () {
  $('#featured-destinations').cSlider({
    loop: false,
    swipe: true,
    fadeDuration: 500
  });
});

Configuration Options

Set options before initialization. The data-loop and data-swipe attributes override their JavaScript counterparts, and subsequent .cSlider() calls on an initialized carousel leave its configuration unchanged.

  • loop (Boolean): Wraps the track to the start when advancing past the last position. Backward navigation always stops at the first position. Default: true.
  • swipe (Boolean): Enables horizontal swipe navigation when Hammer.js is present. Default: true.
  • fadeDuration (Number): Background crossfade duration in milliseconds. The plugin uses a zero-duration fade when reduced motion is active. Default: 300.

HTML attributes and initial active card

Hover and focus select the active card. Arrow clicks and swipes move the card track.

  • data-c-slider: Starts the slider automatically when the DOM is ready.
  • data-loop="false": Turns off forward looping.
  • data-swipe="false": Turns off Hammer.js swipe navigation for this slider.
  • is-active: Marks the initial active card. By default, the first card is active. Choose a card in the initial viewport if you set this class manually; it does not change the track's starting position.

Customize the responsive layout

  • --c-slider-visible: Number of full cards visible at viewport widths of 61.25em and above. Default: 4.
  • --c-slider-height: Slider height at viewport widths of 48em and above. Default: 80vh.

Responsive breakpoints

Edit the media queries to change card widths on phones and tablets or the slider height on phones. The plugin measures card widths at initialization and on window resize.

  • Below 48em: Each card occupies 80% of the slider width. The next card is partially visible when one follows it. Each card displays its own image and summary. The slider height is 70vh.
  • From 48em to below 61.25em: Two cards share the background. Only the active card displays its summary.
  • At 61.25em and above: The --c-slider-visible variable controls card width.

Alternatives and Related Resources


This awesome jQuery plugin is developed by duvec20. For more Advanced Usages, please check the demo page or visit the official website.