【问题标题】:change react-slick slider Dots icon dynamically with user preference in React在 React 中根据用户偏好动态更改 react-slick 滑块点图标
【发布时间】:2019-12-13 17:26:57
【问题描述】:

您好,我使用 react-slick 滑块进行了一些自定义。

我有两个 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} />;
      },
  };

是否可以添加一个活动图标,需要将默认点图标更改为活动点图标

【问题讨论】:

    标签: javascript reactjs react-slick


    【解决方案1】:

    你可以试试这个,只要用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}
              />
            );
          }
        };
    
    

    【讨论】:

    • 正如我提到的,图标会动态变化。用户可以从管理部分更改图标将作为 API 数据记录。不能在样式表中硬编码。请回读问题
    • 太棒了。我忘记了 beforeChange 事件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    相关资源
    最近更新 更多