【问题标题】:Best way to configure Reactstrap carousel images to be responsive将 Reactstrap 轮播图像配置为响应式的最佳方法
【发布时间】:2018-09-15 19:57:10
【问题描述】:

我已经做了很多研究,但是关于 reactstrap 轮播图像响应性的文档却少得惊人。

我的 ReactStrap 轮播响应式调整大小,但图像没有。

我研究/尝试过的选项:

  1. 通过轮播组件本身中的 className 的 CSS?这是我认为可能最好的一个,但我还没有找到适当调整图像大小的背景大小、高度和最大宽度的组合。

  2. srcset ?鉴于轮播是一个组件,我不确定如何实现此属性或任何其他内联属性

  3. 也许在轮播组件本身的某个地方?

  4. 或者有没有更好的方法让我修改图像?

  5. 或者@media 是 css 的答案?

`

const items = [
  {
    src: 'img1.png',
    altText: '',
    caption: ''
  },
  {
    src: 'img2.png',
    altText: '',
    caption: 'Freedom Isn\'t Free'
  },
  {
    src: 'img3.png',
    altText: '',
    caption: ''
  }
];

class HomeCarousel extends Component {
  constructor(props) {
    super(props);
    this.state = { activeIndex: 0 };
    this.next = this.next.bind(this);
    this.previous = this.previous.bind(this);
    this.goToIndex = this.goToIndex.bind(this);
    this.onExiting = this.onExiting.bind(this);
    this.onExited = this.onExited.bind(this);
  }

  onExiting() {
    this.animating = true;
  }

  onExited() {
    this.animating = false;
  }

  next() {
    if (this.animating) return;
    const nextIndex = this.state.activeIndex === items.length - 1 ? 0 : this.state.activeIndex + 1;
    this.setState({ activeIndex: nextIndex });
  }

  previous() {
    if (this.animating) return;
    const nextIndex = this.state.activeIndex === 0 ? items.length - 1 : this.state.activeIndex - 1;
    this.setState({ activeIndex: nextIndex });
  }

  goToIndex(newIndex) {
    if (this.animating) return;
    this.setState({ activeIndex: newIndex });
  }

  render() {
    const { activeIndex } = this.state;

    const slides = items.map((item) => {
      return (
        <CarouselItem
          className="carouselImg"
          onExiting={this.onExiting}
          onExited={this.onExited}
          key={item.src}
        >
          <img src={item.src} alt={item.altText} />
          <CarouselCaption captionText={item.caption} captionHeader={item.caption} />
        </CarouselItem>
      );
    });

    return (
      <Carousel
        activeIndex={activeIndex}
        next={this.next}
        previous={this.previous}
      >
        <CarouselIndicators items={items} activeIndex={activeIndex} onClickHandler={this.goToIndex} />
        {slides}
        <CarouselControl direction="prev" directionText="Previous" onClickHandler={this.previous} />
        <CarouselControl direction="next" directionText="Next" onClickHandler={this.next} />
      </Carousel>
    );
  }
}


export default HomeCarousel;

`

【问题讨论】:

    标签: css image reactjs responsive-design reactstrap


    【解决方案1】:

    基于本页https://reactstrap.github.io/components/carousel/的reactstrap Carousel示例1

    这是我如何让它在我的用例中响应:

            <CarouselItem
              onExiting={() => setAnimating(true)}
              onExited={() => setAnimating(false)}
              key={item.src}
            >
              <img src={item.src} alt={item.altText} style={{ width: "100%"}}/>
              <CarouselCaption captionText={item.caption} captionHeader={item.caption} />
            </CarouselItem>
    
    

    因此,将style={{ width: "100%"}} 传递给img 标签,使我的超大图像完美适合屏幕,并且可能适用于其他来到这里的人。

    【讨论】:

      【解决方案2】:

      鉴于 bootstrap 使用 flexbox,您可以通过将以下内容添加到您的 css 来使 reactstrap 轮播图像响应:

      .carousel-item, .carousel-item.active {
          align-items:center;
      }
      

      这似乎可以防止图像高度拉伸。为我工作!

      【讨论】:

        【解决方案3】:

        你好,你好,

        我已经在我的 reactjs 应用程序中使用 Carousel 组件尝试了这个 reactstrap。 我通过添加引导程序 4 类“d-block w-100”解决了这个问题。

        我在我的 reactstrap Carousel 组件和这个元素中创建了这个

        从此:

        <img src={item.src} alt={item.altText} />
        

        我改成:

        <img className="d-block w-100" src={item.src} alt={item.altText} />
        

        我刚刚从 bootstrap 4 文档中复制了这些类(d-block w-100) https://getbootstrap.com/docs/4.0/components/carousel/

        这是我将 Reactstrap Carousel 与来自我的 WordPress Rest API 数据的动态数据一起使用的示例。 https://github.com/jun20/React-JS-and-WP-Rest-API/tree/home-carousel-final

        【讨论】:

          【解决方案4】:
          .carousel-item > img { 
            width: 100%; 
          }
          

          ... 将解决您的问题。
          它与 Reactstrap 无关。这可能就是你没有找到太多东西的原因。仅与 TwBs Carousel 有关。我个人不认为该规则不是 TwB 轮播 CSS 的一部分的任何理由,因为每个人都希望 &lt;img&gt; 具有其父级的 100% 宽度。

          如果您想将其限制为特定的轮播,请相应地修改选择器。


          另一个经常被请求的 TwBs 轮播模组是:

          .carousel-control-prev,.carousel-control-next {
            cursor:pointer;
          }
          

          【讨论】:

          • 这就是答案。为只见树木不见森林而自责。还要感谢您提供其他 TwBs 轮播模式。将鼠标悬停在箭头上时,让光标变为活动状态非常好。
          猜你喜欢
          • 1970-01-01
          • 2019-06-18
          • 2017-07-29
          • 1970-01-01
          • 2021-02-04
          • 2013-04-01
          • 2020-09-19
          • 2019-06-16
          • 2016-05-05
          相关资源
          最近更新 更多