在最小宽度

时间:2017-09-14 03:53:01

标签: reactjs react-slick

我正在尝试禁用最小宽度的反应滑块:768px,但似乎反应光滑滑块仅支持最大宽度。我在光滑的设置中使用responsive属性,根据文档,这将采用断点与该断点的设置。然而,断点是最大宽度而不是最小宽度,我试图禁用旋转木马' unslick'在断点768px。有没有办法可以在最小宽度处禁用旋转木马:768而不是最大宽度?

    var settings = {
        dots: true,
        infinite: true,
        repsponsive: [
            {
                breakpoint: 768,
                settings: 'unslick'
            }
        ],
        speed: 500,
        slidesToShow: 1,
        slidesToScroll: 1
    };

1 个答案:

答案 0 :(得分:2)

我查看了source code for react-slick并找到了下面显示的代码部分;

 if (this.props.responsive) {
      var breakpoints = this.props.responsive.map(breakpt => breakpt.breakpoint);
      breakpoints.sort((x, y) => x - y);

      breakpoints.forEach((breakpoint, index) => {
        var bQuery;
        if (index === 0) {
          bQuery = json2mq({minWidth: 0, maxWidth: breakpoint});
        } else {
          bQuery = json2mq({minWidth: breakpoints[index-1], maxWidth: breakpoint});
        }
        this.media(bQuery, () => {
          this.setState({breakpoint: breakpoint});
        });
      });

      // Register media query for full screen. Need to support resize from small to large
      var query = json2mq({minWidth: breakpoints.slice(-1)[0]});

      this.media(query, () => {
        this.setState({breakpoint: null});
     });
 }

我从这段代码中了解到react-slick实际上正在同时使用min-width and max-width,但在对给定的断点进行排序后,首先会得到min-width: 0

所以这是你可以做的事情。它有点破解,但我认为它可以完成这项任务。

  const settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1,
      responsive: [
        {
          breakpoint: 768,
        },
        {
          breakpoint: 10000, // a unrealistically big number to cover up greatest screen resolution
          settings: 'unslick'
        }
      ]
    };

此代码的作用是,它创建了2个媒体条目,

@media only screen and (min-width: 0px) and (max-width: 768px) { ... } // empty
@media only screen and (min-width: 768px) and (max-width: 10000px) { ... } // disables slick

希望它有所帮助。