为什么我的React Slick轮播响应性不起作用?

huangapple go评论78阅读模式
英文:

Why is my react slick carousel responsiveness not working?

问题

import React from "react";
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import Link from "next/link";

function SampleNextArrow(props) {
  const { className, style, onClick } = props;
  const updatedStyle = {
    ...style,
    display: "block",
    background: "grey",
    padding: "3px",
    width: "25px",
    height: "25px",
  };
  return (
    <div className={className} style={updatedStyle} onClick={onClick} />
  );
}

function SamplePrevArrow(props) {
  const { className, style, onClick } = props;
  const updatedStyle = {
    ...style,
    display: "block",
    background: "grey",
    padding: "3px",
    width: "25px",
    height: "25px",
  };

  return (
    <div className={className} style={updatedStyle} onClick={onClick} />
  );
}

export default function SimpleSlider({ slides, slidesToShow, centerPadding }) {

  var settings = {
    dots: true,
    infinite: true,
    speed: 2000,
    arrows: true,
    autoplay: true,
    autoplaySpeed: 3000,
    slidesToShow: slidesToShow,
    pauseOnHover: true,
    centerMode: true,
    centerPadding: centerPadding,
    nextArrow: <SampleNextArrow />,
    prevArrow: <SamplePrevArrow />,
    responsive: [
      {
        breakpoint: 1024,
        settings: {
          slidesToShow: 3,
          slidesToScroll: 3,
          infinite: true,
          dots: true,
          arrows: false,
        },
      },
      {
        breakpoint: 600,
        settings: {
          slidesToShow: 2,
          slidesToScroll: 2,
          initialSlide: 2,
          arrows: false,
        },
      },
      {
        breakpoint: 480,
        settings: {
          slidesToShow: 1,
          slidesToScroll: 1,
          arrows: false,
        },
      },
    ],
    appendDots: (dots) => (
      <ul
        style={{
          display: "inline-block",
          margin: "0 5px",
          padding: "5px",
          borderRadius: "25%",
          cursor: "pointer",
        }}
      >
        {dots}
      </ul>
    ),
  };

  return (
    <>
      <Slider  {...settings} className="mb-10 rounded-xl relative">
        {slides.map((slide) => (
          <div className="h-[16rem] py-[1rem] px-[0.25rem]" key={slide.id}>
            <Link href={slide.link}>
              <img
                className="w-full rounded-xl hover:scale-110"
                src={slide.backdrop_path}
                alt={`slide ${slide.id}`}
              />
            </Link>
          </div>
        ))}
      </Slider>
    </>
  );
}

I was trying to implement this react slick carousel to my Next.js project, but for some reason, the responsiveness isn't working. Whenever I reduce my screen size to mobile view (let's say 480px), it vanishes from the position it's supposed to be, and instead, when I increase the size, it starts displaying all slides at once.

Here is the component call:

<Test2
  slides={slideData}
  slidesToShow={1} 
  centerPadding="300px" 
/>
英文:

为什么我的React Slick轮播响应性不起作用?

import React from &quot;react&quot;;
import Slider from &quot;react-slick&quot;;
import &quot;slick-carousel/slick/slick.css&quot;;
import &quot;slick-carousel/slick/slick-theme.css&quot;;
import Link from &quot;next/link&quot;;
function SampleNextArrow(props) {
const { className, style, onClick } = props;
const updatedStyle = {
...style,
display: &quot;block&quot;,
background: &quot;grey&quot;,
padding: &quot;3px&quot;,
// borderRadius: &quot;50%&quot;,
width: &quot;25px&quot;,
height: &quot;25px&quot;,
};
return (
&lt;div
className={className}
style={updatedStyle}
onClick={onClick}
/&gt;
);
}
function SamplePrevArrow(props) {
const { className, style, onClick } = props;
const updatedStyle = {
...style,
display: &quot;block&quot;,
background: &quot;grey&quot;,
padding: &quot;3px&quot;,
//   borderRadius: &quot;50%&quot;,
width: &quot;25px&quot;,
height: &quot;25px&quot;,
};
return (
&lt;div
className={className}
style={updatedStyle}
onClick={onClick}
/&gt;
);
}
export default function SimpleSlider({ slides, slidesToShow, centerPadding }) {
var settings = {
// className: &quot;center&quot;,
dots: true,
infinite: true,
speed: 2000,
arrows: true,
autoplay: true,
autoplaySpeed: 3000,
slidesToShow: slidesToShow,
pauseOnHover: true,
centerMode: true,
centerPadding: centerPadding,
nextArrow: &lt;SampleNextArrow /&gt;,
prevArrow: &lt;SamplePrevArrow /&gt;,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true,
arrows: false,
}
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2,
initialSlide: 2,
arrows: false,
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
arrows: false,
}
}
],
appendDots: (dots) =&gt; (
&lt;ul
style={{
display: &quot;inline-block&quot;,
margin: &quot;0 5px&quot;,
padding: &quot;5px&quot;,
borderRadius: &quot;25%&quot;,
cursor: &quot;pointer&quot;,
}}
&gt;
{dots}
&lt;/ul&gt;
),
};
return (
&lt;&gt;  
&lt;Slider  {...settings} className=&quot;mb-10 rounded-xl relative&quot;&gt;
{slides.map((slide) =&gt; (
&lt;div className=&quot;h-[16rem] py-[1rem] px-[0.25rem]&quot; key={slide.id}&gt;
&lt;Link href={slide.link}&gt;
&lt;img
className=&quot;w-full rounded-xl hover:scale-110&quot;
src={slide.backdrop_path}
alt={`slide ${slide.id}`}
/&gt;
&lt;/Link&gt;
&lt;/div&gt;
))}
&lt;/Slider&gt;
&lt;/&gt;
);
}

I was trying to implement this react slick carousel to my Next JS project but for some reason, the responsiveness isn't working
whenever I low my screen size for mobile view (lets say 480px) it vanishes from the position its supposed to and instead when I increase size, it starts displaying all slides at once

here is the component call

&lt;Test2
slides={slideData}
slidesToShow={1} 
centerPadding=&quot;300px&quot; 
/&gt;

`

答案1

得分: 0

看起来你应该像这样修改你的设置。

responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true,
arrows: false,
},
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2,
initialSlide: 2,
arrows: false,
centerPadding: "50px", // 在较小的屏幕上添加这一行以减少 centerPadding
},
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
arrows: false,
centerPadding: "0px", // 在最小的屏幕上添加这一行以移除 centerPadding
},
},
],
英文:

It seems you should modify your settings like this.

responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true,
arrows: false,
},
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2,
initialSlide: 2,
arrows: false,
centerPadding: &quot;50px&quot;, // Add this line to reduce centerPadding for smaller screens
},
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
arrows: false,
centerPadding: &quot;0px&quot;, // Add this line to remove centerPadding for smallest screens
},
},
],

huangapple
  • 本文由 发表于 2023年7月17日 15:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定