akiran / react-slick

React carousel component
http://react-slick.neostack.com/
MIT License
11.75k stars 2.11k forks source link

Cumulative Layout Shift #2091

Open lucasoz opened 3 years ago

lucasoz commented 3 years ago

How we could avoid Cumulative Layout Shift? I have a carousel in my web page. I am getting bad CLS score because elements on the carousel is changing size over page load. image image

I am using this settings

{
  dots: false,
  infinite: true,
  slidesToShow: 5,
  slidesToScroll: 1,
  autoplay: true,
  autoplaySpeed: 3000,
  nextArrow: <NextArrow />,
  prevArrow: <PrevArrow />,
  responsive: [
    {
      breakpoint: 1280,
      settings: {
        slidesToShow: 4,
      },
    },
    {
      breakpoint: 1024,
      settings: {
        slidesToShow: 3,
      },
    },
    {
      breakpoint: 600,
      settings: {
        slidesToShow: 2,
      },
    },
    {
      breakpoint: 480,
      settings: {
        slidesToShow: 2,
      },
    },
  ],
}
masoudmoghaddari commented 2 years ago

You should calculate the height of the carousel wrapper element and reserve it before the carousel is loaded to avoid any layout shift. One way is to know your images aspect ratio and calculate the height from width.