Logo

dev-resources.site

for different kinds of informations.

#LearnedToday: Slider in pure CSS

Published at
1/16/2024
Categories
webdev
css
learnedtoday
slider
Author
danielzotti
Categories
4 categories in total
webdev
open
css
open
learnedtoday
open
slider
open
Author
11 person written this
danielzotti
open
#LearnedToday: Slider in pure CSS

🏞️ Do you remember when you had to use heavy JS libraries to create image slideshows?

💪 Those days are gone! You can do it in pure CSS now! (experimental in Chrome, Edge and Opera)

Main concepts

  • Scroll timeline: define a name and a direction for the scrollable element on which the shorthand CSS scroll-timeline property is applied.
  • Animation Timeline: The animation-timeline CSS property specifies the timeline (defined by the scroll-timeline property) that is used to control the progress of a CSS animation.
  • Timeline scope: The timeline-scope CSS property modifies the scope of a named animation timeline.
  • Animation range: The animation-range CSS shorthand property is used to set the start and end of an animation's attachment range along its timeline, i.e. where along the timeline an animation will start and end.

The idea

  • Associate an horizontal (x axis) scroll timeline to the element that contains the slides (.slider__entries) using the scroll-timeline property (it's a sort of event listener of the scroll event for CSS)
.slider__entries {
  scroll-timeline-name: --carousel;
  scroll-timeline-axis: x; // horizontal
}
Enter fullscreen mode Exit fullscreen mode
  • Raise the scope of the timeline from the child (.slider__entries) to the parent (.slider) using the timeline-scope property
.slider {
  timeline-scope: --carousel;
}
Enter fullscreen mode Exit fullscreen mode
  • Associate the scroll animation to the single "dot" in order to highlight the correct "dot"
@keyframes color-bullet {
  from,
  to {
    background: var(--dot-color--active);
  }
}
.dot a {
  animation: color-bullet linear;
  animation-timeline: --carousel;
  animation-range: calc((var(--i) - 1) / 5 * 100%) calc(var(--i) / 5 * 100.01%); // NB: 5 is because we have 5 images in the example!
}
Enter fullscreen mode Exit fullscreen mode
  • Adding a "snap" effect during the scroll and hide the scrollbar
.slider__entries {
  scroll-snap-type: x mandatory; // horizontal
  scrollbar-width: none; // hide scrollbar
  scroll-behavior: smooth; // scroll smoothly
}
Enter fullscreen mode Exit fullscreen mode
  • Adding the slide switch when clicking on the button
<!-- DOT: --> 
<a href="#slide_01"></a>

<!-- SLIDE: -->
<a name="slide_01"><img src="..." /></a>
Enter fullscreen mode Exit fullscreen mode

Demo

I created as usual a working demo.

slider Article's
30 articles in total
Favicon
infinity slider
Favicon
Build A Stunning Animated Travel Image Slider Using Html, Css, And JavaScript!
Favicon
Slider in Flutter
Favicon
SLIDER ANIMATION HELP
Favicon
Making a slider puzzle in Java Script
Favicon
#LearnedToday: Slider in pure CSS
Favicon
Flutter - Triangular CAROUSEL Slider
Favicon
looking for a slider package for angular
Favicon
Creating a Slider using Next.js and CSS
Favicon
Create Dynamic Image Slider with PHP and MySQL
Favicon
A step-by-step guide to creating a beautiful slider with Swiper and React
Favicon
Animating using sliders in JavaFX and SceneBuilder
Favicon
Product Gallery Slider for WooCommerce
Favicon
What is a slider and why you should slider on your website?
Favicon
Kaydırıcı robot
Favicon
Basic JavaScript slider ready source
Favicon
Pure CSS Vertical Image Slider Using Only HTML & CSS
Favicon
How to make a simple slider component in React
Favicon
Responsive Image slider using Owl Carousel | HTML & CSS
Favicon
Simple Slider on a pure JavaScript
Favicon
Image Slideshow HTML CSS
Favicon
Ngx Slick Carousel In Angular Material Card with Custom Arrows
Favicon
How to make a Full Slider with javascript in 10 mins
Favicon
Let's build a slider from scratch in React Native
Favicon
Elegant & Beautiful Testimonial Sliders
Favicon
Choppy Slider: The Styles
Favicon
Flutter. A Quarter Round Slider.
Favicon
VueJS: Double range slider component
Favicon
Styling Range Sliders with CSS
Favicon
AWS – Using Multiple (Staging, UAT and Production) Accounts Under AWS Organization

Featured ones: