Slick Slider React - 无论屏幕尺寸如何,如何保持相同的宽度和空间?

发布于 2025-01-11 07:05:41 字数 2313 浏览 9 评论 0原文

我正在创建一个与 Slick Slider 反应的滑块。问题是:当屏幕尺寸发生变化时,幻灯片尺寸会减小或相互堆叠。

光滑的滑块提供:

      responsive: [
        {
          breakpoint: 1024,
          settings: {
            slidesToShow: 3,
            slidesToScroll: 3,
            infinite: true,
            dots: true
          }, 
    …
      ]

但这并不能阻止幻灯片大小的变化,也不能固定不断变化的幻灯片之间的边距。

我尝试过:

.slick-slide {
  width: auto !important;
}

在 CSS 文件中,但随后幻灯片一层一层地累积,生成 4 行而不是 2 行。此外,动画太大,在滑块末尾创建了一个空白区域。

是否可以创建一个带有幻灯片的滑块,无论窗口大小如何,它们之间都保持相同的大小和空间?有一种方法可以保持幻灯片的固定大小,仅显示屏幕上可能的幻灯片数量并仅显示以下幻灯片的一部分(或不显示)?

以便也能做出反应。想要什么: 输入图片这里的描述

这是我的代码:

      const settings = {
        rows: 2,
        slidesPerRow: 1,
        slidesToShow: 3.2,
        infinite: false,
        swipeToSlide: true,
        slidesToScroll: 1,
        lazyLoad: true,
        responsive: [
            {
              breakpoint: 1200,
              settings: {
                  slidesToShow: 2.5,
                  slidesToScroll: 2,
              }
            },
            {
                breakpoint: 1024,
                settings: {
                    slidesToShow: 2,
                    slidesToScroll: 2,
                }
            },
            {
                breakpoint: 768,
                settings: {
                    slidesToShow: 1.7,
                    slidesToScroll: 1,
                    arrows: false                }
            },
            {
                breakpoint: 480,
                settings: {
                    slidesToShow: 1.4,
                    slidesToScroll: 1,
                    arrows: false
                }
            }
        ]

      };
     
      return (
        <>
          <h2>…</h2>
          <div className="cell">
            <Slider {...settings}>
                {
                    this.props.data.nodes.map((node,key) => (
                        <Cell node={node} key={key} />
                    ))
                } 
            </Slider>
          </div>
        </>
      );

谢谢!

I am creating a slider in react with Slick Slider. The problem is: when the screen size changes, the slides reduce in size or stack on top of each other.

Slick slider offers :

      responsive: [
        {
          breakpoint: 1024,
          settings: {
            slidesToShow: 3,
            slidesToScroll: 3,
            infinite: true,
            dots: true
          }, 
    …
      ]

But this does not prevent the change in size of the slides and does not fix the margin between the slides which are constantly changing.

I tried :

.slick-slide {
  width: auto !important;
}

In the CSS file, but then the slides accumulate one under the other, generating 4 rows instead of 2. Also, the animation is too large and create a blank space at the end of the slider.

Is it possible to create a slider with slides, that keep the same size and space between them whatever the size of the window? A way to keep a fixed size of slides, showing only the number of slides possible on the screen and showing only a part (or not) of the following slides?

So as to be responsive as well. What is wanted :
enter image description here

Here is my code :

      const settings = {
        rows: 2,
        slidesPerRow: 1,
        slidesToShow: 3.2,
        infinite: false,
        swipeToSlide: true,
        slidesToScroll: 1,
        lazyLoad: true,
        responsive: [
            {
              breakpoint: 1200,
              settings: {
                  slidesToShow: 2.5,
                  slidesToScroll: 2,
              }
            },
            {
                breakpoint: 1024,
                settings: {
                    slidesToShow: 2,
                    slidesToScroll: 2,
                }
            },
            {
                breakpoint: 768,
                settings: {
                    slidesToShow: 1.7,
                    slidesToScroll: 1,
                    arrows: false                }
            },
            {
                breakpoint: 480,
                settings: {
                    slidesToShow: 1.4,
                    slidesToScroll: 1,
                    arrows: false
                }
            }
        ]

      };
     
      return (
        <>
          <h2>…</h2>
          <div className="cell">
            <Slider {...settings}>
                {
                    this.props.data.nodes.map((node,key) => (
                        <Cell node={node} key={key} />
                    ))
                } 
            </Slider>
          </div>
        </>
      );

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2025-01-18 07:05:41

例如)

1800px 中的 4 张幻灯片

slipsToShow: _.round((width * 4) / 1800, 1 or 2);

https://react-slick.neostack.com/docs/example/可变宽度/

ex)

4 slides in 1800px

slidesToShow: _.round((width * 4) / 1800, 1 or 2);

or

https://react-slick.neostack.com/docs/example/variable-width/

GRAY°灰色天空 2025-01-18 07:05:41

有一个“可变宽度”选项可用于获得这种结果。查看更多: https://react-slick.neostack.com/docs/example /可变宽度

There is a "Variable width" option that can be used to get this kind of result. See more: https://react-slick.neostack.com/docs/example/variable-width

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文