简体   繁体   English

在 React 中根据用户偏好动态更改 react-slick 滑块 Dots 图标

[英]change react-slick slider Dots icon dynamically with user preference in React

Hi I have some customize using react-slick slider.嗨,我使用 react-slick 滑块进行了一些自定义。

I have two png dot icons and it can be changed dynamically ( user can change from admin section and return front end as API data)我有两个 png 点图标,它可以动态更改(用户可以从管理部分更改并将前端作为 API 数据返回)

../navigator.png 
and 
../navigator-active.png

    sliderSettings = {
      dots: true,
      infinite: true,
      slidesToShow: 1,
      slidesToScroll: 1,
      autoplay: true,
      appendDots: dots => {
        return <ul style={{ margin: '0px' }}>{dots}</ul>;
      },
      customPaging: (pagi, i) => {
        const style = {
            width: 13,
            height: 13,
            display: 'inline-block',
            backgroundImage: `url(../navigator.png )`, // need to change as navigator-active.png this when active
            backgroundSize: 'contain',
            backgroundRepeat: 'no-repeat',
        };
        return <a className="slick-dot" style={style} />;
      },
  };

Is that possible to way add an active icon, need to change default dot icon to active dot icon是否可以添加活动图标,需要将默认点图标更改为活动点图标

You can try this, just record the current silde index using setState, and change the style according to it.你可以试试这个,只要用setState记录当前silde索引,并根据它改变样式即可。

 render() {
    var settings = {
      dots: true,
      infinite: true,
      slidesToShow: 1,
      slidesToScroll: 1,
      autoplay: true,
      appendDots: dots => {
        return <ul style={{ margin: "0px" }}>{dots}</ul>;
      },
      beforeChange: (prev, next) => {   // here to detect slide change
        this.setState({ currentSlideIndex: next });
      },
      customPaging: (pagi, i) => {
        const style = {
          width: 13,
          height: 13,
          display: "inline-block",
          backgroundImage: `url(../navigator.png )`, // need to change as navigator-active.png this when active
          backgroundSize: 'contain',
          backgroundRepeat: "no-repeat"
        };
        const activeStyle = {
          width: 13,
          height: 13,
          display: "inline-block",
          backgroundImage: `url(../navigator-active.png )`,
          backgroundSize: 'contain',
          backgroundRepeat: "no-repeat"
        };
        return (
          <a
            className="slick-dot"
            style={pagi === this.state.currentSlideIndex ? activeStyle : style}
          />
        );
      }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM