Skip to content

Slider JS

scss
@use '@natachah/vanilla-frontend/scss/components/slider';

Syntax

The slider is using the a <div> tag with the class .slider.

Each slide must be a <div> with role="tabpanel" and aria-hidden="true" attributes.

html
<div id="slider" class="slider">
    <div id="slide1" role="tabpanel" aria-hidden="false"><img src="https://picsum.photos/id/1/300" alt="Image 1"></div>
    <div id="slide2" role="tabpanel" aria-hidden="true"><img src="https://picsum.photos/id/2/300" alt="Image 2"></div>
    <div id="slide3" role="tabpanel" aria-hidden="true"><img src="https://picsum.photos/id/3/300" alt="Image 3"></div>
</div>
<button aria-controls="slider" data-slider-prev>previous</button>
<div aria-controls="slider" role="tablist">
    <button role="tab" aria-controls="slide1" aria-selected="true">1</button>
    <button role="tab" aria-controls="slide2" aria-selected="false">2</button>
    <button role="tab" aria-controls="slide3" aria-selected="false">3</button>
</div>
<button aria-controls="slider" data-slider-next>next</button>
css
--slider-columns
--slider-gap
js
import Slider from "@natachah/vanilla-frontend/js/_slider"
const slider = document.getElementById('slider')
if (slider) const mySliderObj = new Slider(slider, {loop: true})

Indicators

You can create some indicators with a <div> tag with the aria-controls="IdOfSlider" and role="tablist" attributes.

Inside you must insert each slides indicators as <button> tag with role="tab", aria-controls="IdOfSlide" and aria-selected="false" attributes.

html
<div aria-controls="slider" role="tablist">
    <button role="tab" aria-controls="slide1" aria-selected="true">1</button>
    <button role="tab" aria-controls="slide2" aria-selected="false">2</button>
    <button role="tab" aria-controls="slide3" aria-selected="false">3</button>
</div>

You can create some prev/next navigation with some <button> tag with the aria-controls="IdOfSlider" and data-slider-prev or data-slider-next attributes.

html
<button aria-controls="slider" data-slider-prev>previous</button>
<button aria-controls="slider" data-slider-next>next</button>

You must add the play and pause <button> if you use the autoplay version of the slider.

html
<button aria-controls="slider" data-slider-play>play</button>
<button aria-controls="slider" data-slider-pause>pause</button>

Javascript

To work properly the slider required some javascript.

Options

NameDescriptionValue
behaviorThe scroll behavior inside the slidersmooth as string can be auto, smooth or instant
loopIf set to true, the slider will work as a carouselfalse as boolean
autoplayIf set to true, the slider will automatically cyclingfalse as boolean
js
new Slider(mySliderDiv, {
    behavior: 'smooth',
    loop: false,
    autoplay: false
})

Methods

MethodDescription
goTo(index)Go to a specific slide by index starting by 0
next()Go to the next slide
prev()Go to the previous slide

Events

Event nameDescriptionValue
slider:changedThis event is fired when the slide has been changedcurrent as a index number
js
document.getElementById('mySliderId').addEventListener('slider:changed', (e) => {
    const theCurrentIndexSlide = e.detail.current
})

Released under the MIT License.