0

我试图在 min-width: 768px 处禁用反应滑块,但反应光滑滑块似乎只支持最大宽度。我responsive在光滑设置中使用该属性,根据文档,这需要一个带有该断点设置的断点。然而,断点是最大宽度而不是最小宽度,我试图在断点 768px 处使用“unslick”禁用轮播。有没有办法可以在 min-width: 768 而不是 max-width 处禁用轮播?

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

1 回答 1

5

我检查了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.

所以这是你可以做的事情。它有点hack,但我认为它可以完成工作。

  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

希望能帮助到你。

于 2017-09-14T08:50:11.240 回答