1

我在我的项目中使用React-Slick组件。除了最后一张幻灯片,我必须在所有幻灯片上显示分页点。我想隐藏最后一张幻灯片的分页点。

我已经检查过该afterChange函数可用作回调。这可以用于自定义 jQuery 中的滑块 css,但是如何使用此功能隐藏 React 应用程序中的分页点?

import Slider from 'react-slick';

export default class Carousal extends Component {
render() {
    let settings = {
        accessibility: true,
        dots: true,
        infinite: false,
        speed: 500,
        slidesToShow: 1,
        slidesToScroll: 1,
        centerMode: true,
        centerPadding: '10px',
        responsive: [{
            breakpoint: 768,
            settings: {
                swipeToSlide: true
            }
        }],
        afterChange: function (currentSlide) {
           console.log(currentSlide);
        }
    };

    return (
        <Slider {...settings} >
            {slides}
        </Slider>
    );
}
4

1 回答 1

2

在组件构造上,为 Slider 组件的点属性设置状态值。然后在不想显示时将其设置为 false。

export default class Carousal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dots: true
    };
  }
  render() {
    let settings = {
      accessibility: true,
      dots: this.state.dots,
      infinite: false,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1,
      centerMode: true,
      centerPadding: '10px',
      responsive: [{
        breakpoint: 768,
        settings: {
          swipeToSlide: true
        }
      }],
      afterChange: (currentSlide) => {
        console.log(currentSlide);
        this.setState({
          dots: (currentSlide !== 'yourDesiredSlide')
        });
      }
    };

    return (
      <Slider {...settings} >
        {slides}
      </Slider>
    );
  }
于 2017-08-29T19:36:02.570 回答