如何在 React Slick 的 Slider dots 中添加 slick-active 类

saj*_*eyo 3 javascript css reactjs slick.js react-slick

我使用了 react-slick 并使用 Slider 组件制作了一个旋转木马。代码如下,

const carousel = (props) => {
    return (
        <div style={{ height: '50%', marginTop: '20px' }}>
            <Slider {...settings}>
                <div className={text__bar}>
                    <div >
                        <font>Lorum Ipsum 1</font>
                    </div>
                    <div className={slide1} />
                </div>
                <div className={text__bar}>
                    <div>
                        <font>Lorum Ipsum 2</font>
                    </div>
                    <div className={slide1} />
                </div>
                <div className={text__bar}>
                    <div>
                        <font>Lorum Ipsum 3</font>
                    </div>
                    <div className={slide1} />
                </div>
            </Slider>
            <div className={purple__bar}></div>
        </div>
    );
};
Run Code Online (Sandbox Code Playgroud)

和设置对象,

const settings = {
    dots: true,
    infinite: true,
    speed: 500,
    slidesToShow: 1,
    slidesToScroll: 1,
    autoplay: true,
    autoplaySpeed: 5000,
    className: SlickMain,
    dotsClass: button__bar,
    arrows: false,
};
Run Code Online (Sandbox Code Playgroud)

我添加了一些额外的 CSS 来设计我的旋转木马点(按钮)看起来像波纹管,

点栏

当开发人员工具检查与当前显示的幻灯片相关的按钮时,会注入一个名为“slick-active”的 CSS 类

注入类

我需要做的是将与幻灯片对应的按钮的背景颜色更改为黑色,使当前颜色为紫色。

我所做的是,

.button__bar li.slick-active button {
    opacity: .75;
    color: #000
}
Run Code Online (Sandbox Code Playgroud)

但它不会工作。我错过了什么?

.button__bar是我在设置对象中给点的 dotsClass。

saj*_*eyo 6

发生这种情况是因为我使用 CSS-Modules 加载我的 CSS 文件。在下面的代码示例中,您可以清楚地看到发生了什么。

https://codesandbox.io/s/1z2lnox5rq?fontsize=14

因此,作为解决方案,我所做的是向设置对象添加波纹管方法。

const settings = {
  dots: true,
  infinite: true,
  speed: 1000,
  slidesToShow: 1,
  slidesToScroll: 1,
  autoplay: true,
  autoplaySpeed: 3000,
  className: slickMain,
  dotsClass: button__bar,
  arrows: false,
  beforeChange: (prev, next) => {
    this.setState({ currentSlide: next });
  },
  // afterChange: (index) => this.setState({ currentSlide: index }),
  appendDots: dots => {
    return (
      <div>
        <ul>
          {dots.map((item, index) => {
            return (
              <li key={index}>{item.props.children}</li>
            );
          })}
        </ul>
      </div>
    )
  },
  customPaging: index => {
    return (
      <button style={index === this.state.currentSlide ? testSettings : null}>
        {index + 1}
      </button>
    )
  }
};
Run Code Online (Sandbox Code Playgroud)

为了解决我的问题,我曾经beforeChange获取下一张幻灯片索引并将其保存在状态中。然后使用appendDotsadivul,li被注入到点栏部分。在 内部li,abutton作为子道具使用customPaging方法传递。当当前幻灯片等于处于状态的 的索引时customPaging,则为类注入背景色。

const testSettings = {
    backgroundColor: 'rgba(255, 255, 255, 0.8)',
    outline: '0'
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*nko 5

如果有人使用 Material ui,这里是它的工作原理

const settings: Settings = {
  dots: true,
  infinite: true,
  speed: 500,
  slidesToShow: 1,
  slidesToScroll: 1,
  dotsClass: `slick-dots ${classes.dots}`,
};
// your classes created with material ui
makeStyles((theme: Theme) => ({
// may not be the best way but it works
  dots: {
    bottom: 0,
    "& li.slick-active button::before": {
      color: theme.palette.primary.main,
    },
    "& li": {
      "& button::before": {
      fontSize: theme.typography.pxToRem(14),
      color: "#fff",
      opacity: 0.5,
    },
  }
}))
Run Code Online (Sandbox Code Playgroud)

这是一个codesandbox链接 https://codesandbox.io/s/bold-snow-h9rwq?file=/src/App.tsx

请注意,我还没有通过导入 slick css ie 检查它

// Import css files
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
Run Code Online (Sandbox Code Playgroud)

但改用cdn