【问题标题】:How to add slick-active class in Slider dots in React Slick如何在 React Slick 的 Slider 点中添加 slick-active 类
【发布时间】:2019-07-25 21:10:04
【问题描述】:

我使用 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>
    );
};

还有设置对象,

const settings = {
    dots: true,
    infinite: true,
    speed: 500,
    slidesToShow: 1,
    slidesToScroll: 1,
    autoplay: true,
    autoplaySpeed: 5000,
    className: SlickMain,
    dotsClass: button__bar,
    arrows: false,
};

我添加了一些额外的 CSS 来设置我的轮播点(按钮)的样式,如下所示,

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

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

我所做的是,

.button__bar li.slick-active button {
    opacity: .75;
    color: #000
}

但这行不通。我错过了什么?

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

【问题讨论】:

  • 您是否需要设置.button__bar li.slick-active 的样式而不是.button__bar li.slick-active button?除非按钮确实存在于li 中。
  • buttonli
  • 查看示例按钮样式是使用:before 伪类应用的,你试过吗? .button__bar li.slick-active button:before { opacity: .75; color: #000 }
  • 可以,不需要申请before
  • 你想在这里使用dotsClass: 'button__bar'而不是button__bar对象吗?

标签: javascript css reactjs slick.js react-slick


【解决方案1】:

发生这种情况是因为我正在使用 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>
    )
  }
};

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

const testSettings = {
    backgroundColor: 'rgba(255, 255, 255, 0.8)',
    outline: '0'
}

【讨论】:

    【解决方案2】:

    如果有人在这里使用材质 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,
        },
      }
    }))
    

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

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

    // Import css files
    import "slick-carousel/slick/slick.css";
    import "slick-carousel/slick/slick-theme.css";
    

    但使用 cdn 代替

    【讨论】:

      【解决方案3】:

      我已经使用 :global() 使用 CSS 模块完成了这项工作

      在您的具体情况下添加li.slick-active --> :global(li.slick-active)

      .button__bar :global(li.slick-active) button {
        opacity: .75;
        color: #000
      }
      

      这里是 codebox 的分支

      【讨论】:

        猜你喜欢
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-23
        相关资源
        最近更新 更多