Super fast lightweight sliders and carousels

Sliders and Carousels

Sliders and carousels are on most websites used for presenting all kinds of content – and especially also to show images for i.e. product details etc.

They all need to work really well on mobile – meaning that touch su…


This content originally appeared on DEV Community and was authored by Nicolai Høeg Pedersen

Sliders and Carousels

Sliders and carousels are on most websites used for presenting all kinds of content - and especially also to show images for i.e. product details etc.

They all need to work really well on mobile - meaning that touch support, performance and UX has to be really good.

Old world

The solution to this is a using some sort of javascript library that handles this. Most of these work using a number of events on the DOM to detect touch, movement, dragging etc. And then a good chunk of code to handle the actual sliding using animations and moving around with html elements.

The result of this approach are some relatively big javascript libraries that easily takes 25-100kb - and because of the amount of js, it is not easy to get really good performance, WCAG support is somewhat lagging and setup is relatively complex.

New world

Luckily browsers evolve quickly and the sliding and carousel experience can be handled using native browser features - and simple div overflows.

By utilizing scrolling, especially the touch support can be greatly improved since all browsers, devices and their input methods supports scrolling.

Also loading, event listeners, animations, moving divs around are no longer needed - the browser takes care of this.

The result - super performance and touch support!

The basic approach

  • Create a div with overflow-x set to auto
  • Indside that div, create a CSS grid using display:grid and grid-* options to create columns that will be the slides
  • Use scroll snapping by utilizing scroll-snap-type: x mandatory and scroll-snap-align to ensure slides stick to the sliding container.
<style>
    .slider {
        overflow-x: auto;
        scroll-snap-type: x mandatory;
        scroll-behavior: smooth;
        scrollbar-width: none;
        display: grid;
        grid: auto / auto-flow max-content;
        grid-auto-rows: 100%;
        grid-auto-columns: 100%;
        grid-auto-flow: column;
        grid-gap: 1rem;
        align-items: center;
        height: 100%;
    }

    .slider>* {
        scroll-snap-align: start;
    }
</style>

<div class="slider">
    <div>Slide 1</div>
    <div>Slide 2</div>
    <div>Slide 3</div>
    <div>Slide 4</div>
</div>

That's it - and then a little bit of JS on top to enable navigation buttons and indicators, some css to style the thing up and voila!!

Example slider

Based on this concept and bringing it to live on real solutions this is now wrapped and packed in a small css and javascript module.

See examples and documentation at https://swiffyslider.com/

Find it on Github https://github.com/dynamicweb/swiffy-slider

Or take it from NPM:

npm install swiffy-slider


This content originally appeared on DEV Community and was authored by Nicolai Høeg Pedersen


Print Share Comment Cite Upload Translate Updates
APA

Nicolai Høeg Pedersen | Sciencx (2021-11-09T14:46:51+00:00) Super fast lightweight sliders and carousels. Retrieved from https://www.scien.cx/2021/11/09/super-fast-lightweight-sliders-and-carousels/

MLA
" » Super fast lightweight sliders and carousels." Nicolai Høeg Pedersen | Sciencx - Tuesday November 9, 2021, https://www.scien.cx/2021/11/09/super-fast-lightweight-sliders-and-carousels/
HARVARD
Nicolai Høeg Pedersen | Sciencx Tuesday November 9, 2021 » Super fast lightweight sliders and carousels., viewed ,<https://www.scien.cx/2021/11/09/super-fast-lightweight-sliders-and-carousels/>
VANCOUVER
Nicolai Høeg Pedersen | Sciencx - » Super fast lightweight sliders and carousels. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/09/super-fast-lightweight-sliders-and-carousels/
CHICAGO
" » Super fast lightweight sliders and carousels." Nicolai Høeg Pedersen | Sciencx - Accessed . https://www.scien.cx/2021/11/09/super-fast-lightweight-sliders-and-carousels/
IEEE
" » Super fast lightweight sliders and carousels." Nicolai Høeg Pedersen | Sciencx [Online]. Available: https://www.scien.cx/2021/11/09/super-fast-lightweight-sliders-and-carousels/. [Accessed: ]
rf:citation
» Super fast lightweight sliders and carousels | Nicolai Høeg Pedersen | Sciencx | https://www.scien.cx/2021/11/09/super-fast-lightweight-sliders-and-carousels/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.