<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nicolai Høeg Pedersen</title>
    <description>The latest articles on DEV Community by Nicolai Høeg Pedersen (@nicped).</description>
    <link>https://dev.to/nicped</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F748712%2Fa86b5dc5-7875-42e7-b6d7-5ac1f0fb3dee.png</url>
      <title>DEV Community: Nicolai Høeg Pedersen</title>
      <link>https://dev.to/nicped</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nicped"/>
    <language>en</language>
    <item>
      <title>Super fast lightweight sliders and carousels</title>
      <dc:creator>Nicolai Høeg Pedersen</dc:creator>
      <pubDate>Tue, 09 Nov 2021 14:46:51 +0000</pubDate>
      <link>https://dev.to/nicped/super-fast-lightweight-sliders-and-carousels-f4e</link>
      <guid>https://dev.to/nicped/super-fast-lightweight-sliders-and-carousels-f4e</guid>
      <description>&lt;h2&gt;
  
  
  Sliders and Carousels
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;They all need to work really well on mobile - meaning that touch support, performance and UX has to be really good.&lt;/p&gt;

&lt;h3&gt;
  
  
  Old world
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  New world
&lt;/h3&gt;

&lt;p&gt;Luckily browsers evolve quickly and the sliding and carousel experience can be handled using native browser features - and simple div overflows.&lt;/p&gt;

&lt;p&gt;By utilizing scrolling, especially the touch support can be greatly improved since all browsers, devices and their input methods supports scrolling. &lt;/p&gt;

&lt;p&gt;Also loading, event listeners, animations, moving divs around are no longer needed - the browser takes care of this.&lt;/p&gt;

&lt;p&gt;The result - super performance and touch support!&lt;/p&gt;

&lt;h3&gt;
  
  
  The basic approach
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a div with &lt;code&gt;overflow-x&lt;/code&gt; set to auto&lt;/li&gt;
&lt;li&gt;Indside that div, create a CSS grid using &lt;code&gt;display:grid&lt;/code&gt; and grid-* options to create columns that will be the slides&lt;/li&gt;
&lt;li&gt;Use scroll snapping by utilizing &lt;code&gt;scroll-snap-type: x mandatory&lt;/code&gt; and &lt;code&gt;scroll-snap-align&lt;/code&gt; to ensure slides stick to the sliding container.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;style&amp;gt;
    .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&amp;gt;* {
        scroll-snap-align: start;
    }
&amp;lt;/style&amp;gt;

&amp;lt;div class="slider"&amp;gt;
    &amp;lt;div&amp;gt;Slide 1&amp;lt;/div&amp;gt;
    &amp;lt;div&amp;gt;Slide 2&amp;lt;/div&amp;gt;
    &amp;lt;div&amp;gt;Slide 3&amp;lt;/div&amp;gt;
    &amp;lt;div&amp;gt;Slide 4&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;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!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkngmnlj4e9nxpwst8jlv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkngmnlj4e9nxpwst8jlv.png" alt="Example slider"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;See examples and documentation at &lt;a href="https://swiffyslider.com/" rel="noopener noreferrer"&gt;https://swiffyslider.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find it on Github &lt;a href="https://github.com/dynamicweb/swiffy-slider" rel="noopener noreferrer"&gt;https://github.com/dynamicweb/swiffy-slider&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or take it from NPM:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install swiffy-slider


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
