代码之家  ›  专栏  ›  技术社区  ›  JR3652

反应滑溜高度问题

  •  3
  • JR3652  · 技术社区  · 7 年前

    我很难让这些React Slick滑块组件达到相同的高度。它们都是响应性很强的div,并随着页面大小的变化而相应调整大小,但我希望左侧的div与右侧的div高度相同。

    picture that shows differing heights of divs in React-Slick slider

    我用下面的代码片段定义了SimpleSlider。

    class SimpleSlider extends React.Component {
      constructor(props) {
        super(props);
        ...
      }
      render() {
        let settings = {
          dots: true,
          infinite: true,
          arrows: true,
          speed: 500,
          slidesToShow: 2,
          slidesToScroll: 2,
          initialSlide: 0,
          responsive: [...]
        };
        let Components = this.sliderObjects.components;
    
        return (
          <div>
            <h2> {this.sliderObjects.title} </h2>
            <Slider {...settings} >
            {
              Components.map((component,index)=>
               <div key={index}  >
                <SliderComponent key={index} {...component} />
                </div>
                )
            }
            </Slider>
          </div>
        );
      }
    }
    

    然后我的SliderComponent被定义为

    class SliderComponent extends React.Component{
      render() {
        return (
          <div className='slick-slider-component'>
                <h3>{this.props.header}</h3>
                <p>{this.props.text}</p>
          </div>
        );
      }
    }
    

    有人能帮我吗?我觉得这应该是可能的,也不是难上加难,但我想不出来。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Michael Ploeckinger    7 年前

    如果你知道你的最大高度,你可以用一些最小高度(或者css中的最小高度)来设计你的幻灯片组件。 你可以找到一些 input here.

    如果您不知道: 钩住组件将安装:

    componentDidMount() {
        const height = document.getElementById('slidercomponent-id').clientHeight;
        this.setState({ height });
      },
    

    并将此高度设置为您在div中的高度。