4

我有来自休息 api 的图像进入我的应用程序。然后将这些图像填​​充到react-multi-carousel中。我想做的是使这些图像可点击用于灯箱。我对 React 还很陌生。到目前为止,我的轮播滑块代码是:

滑块.js

  import React, { Component } from 'react';
  import '../slider/slider.css';
  import Carousel from "react-multi-carousel";
  import "react-multi-carousel/lib/styles.css";

  const responsive = {
      superLargeDesktop: {
          breakpoint: { max: 4000, min: 3000 },
          items: 1,
      },
      desktop: {
          breakpoint: { max: 3000, min: 1024 },
          items: 1,
      },
      tablet: {
          breakpoint: { max: 1024, min: 464 },
          items: 1,
      },
      mobile: {
          breakpoint: { max: 464, min: 0 },
          items: 1,
      },
  };

  class Slider extends Component {
      _isMounted = false;

      state = {
          awsApiData: [],
          loading: false,
      }

      componentDidMount() {
          this._isMounted = true;
          console.log('app mounted');
          this.setState({ loading: true });
          /*global fetch */
          fetch('https://onelbip0e6.execute-api.eu-west-2.amazonaws.com/xxxxx')
              .then(data => data.json())
              .then(data => this.setState({ awsApiData: data[0], loading: false }, () => console.log(data)));
      }
      componentWillUnmount() {
          this._isMounted = false;
      }

      render() {
          return (
              <div>
              {this.state.loading
                    ? <div className="text-center">Loading...</div>
                    : 
                        <Carousel
                            additionalTransfrom={0}
                            showDots={false}
                            arrows={true}
                            autoPlaySpeed={3000}
                            autoPlay={false}
                            centerMode={false}
                            className="slider"
                            containerClass="container-with-dots"
                            dotListClass="dots"
                            draggable
                            focusOnSelect={false}
                            infinite
                            itemClass="carousel-top"
                            keyBoardControl
                            minimumTouchDrag={80}
                            renderButtonGroupOutside={false}
                            renderDotsOutside
                            responsive={responsive}>
                           {Object.values(this.state.awsApiData).map((post, indx) => {
  return (
      <div className="mt-5" key={indx}>
          <img className="media-img card-img-top card-img-hero" src={post.image} alt="Alt text"></img>

      </div>
  );
})}
                        </Carousel>
              }
                        </div>
          );
      }
  }

  export default Slider;
4

1 回答 1

2

您可以手动制作灯箱,方法是onClick为每个图像发出一个并放大图像,添加标题,全屏等。

如果您可以利用现有的库来为您的轮播实现灯箱功能(例如react-images ),那就容易多了。

这是实现了灯箱功能的代码的工作副本(使用 react-multi-carousel 和 react-images)。

https://codesandbox.io/s/react-multi-corousal-issue-72s9o?file=/src/Slider.js

这是代码片段。

import React, { Component } from "react";
// import "../slider/slider.css";
import "./slider.css";
import Carousel from "react-multi-carousel";
import "react-multi-carousel/lib/styles.css";
import LightBox, { Modal, ModalGateway } from "react-images";

const responsive = {
  superLargeDesktop: {
    breakpoint: { max: 4000, min: 3000 },
    items: 1
  },
  desktop: {
    breakpoint: { max: 3000, min: 1024 },
    items: 1
  },
  tablet: {
    breakpoint: { max: 1024, min: 464 },
    items: 1
  },
  mobile: {
    breakpoint: { max: 464, min: 0 },
    items: 1
  }
};

class Slider extends Component {
  _isMounted = false;

  state = {
    awsApiData: [],
    loading: false,
    selectedIndex: 0,
    lightboxIsOpen: false
  };

  componentDidMount() {
    this._isMounted = true;
    console.log("app mounted");
    this.setState({ loading: true });
    /*global fetch */
    // fetch("https://onelbip0e6.execute-api.eu-west-2.amazonaws.com/xxxxx")
    fetch("https://picsum.photos/v2/list?page=1&limit=10")
      .then(data => data.json())
      .then(data =>
        // this.setState({ awsApiData: data[0], loading: false }, () =>
        this.setState(
          {
            awsApiData: data.map(item => ({ source: item.download_url })),
            loading: false
          },
          () => console.log(data)
        )
      );
  }
  componentWillUnmount() {
    this._isMounted = false;
  }

  toggleLightbox = selectedIndex => {
    this.setState(state => ({
      lightboxIsOpen: !state.lightboxIsOpen,
      selectedIndex
    }));
  };

  render() {
    return (
      <div>
        {this.state.loading ? (
          <div className="text-center">Loading...</div>
        ) : (
          <>
            <Carousel
              additionalTransfrom={0}
              showDots={false}
              arrows={true}
              autoPlaySpeed={3000}
              autoPlay={true}
              centerMode={false}
              className="slider"
              containerClass="container-with-dots"
              dotListClass="dots"
              draggable
              focusOnSelect={false}
              infinite
              itemClass="carousel-top"
              keyBoardControl
              minimumTouchDrag={80}
              renderButtonGroupOutside={false}
              renderDotsOutside
              responsive={responsive}
            >
              {Object.values(this.state.awsApiData).map((post, indx) => {
                return (
                  <div
                    className="mt-5"
                    key={indx}
                    onClick={() => this.toggleLightbox(indx)}
                  >
                    <img
                      className="media-img card-img-top card-img-hero"
                      src={post.source}
                      alt="Alt text"
                    />
                  </div>
                );
              })}
            </Carousel>
            <ModalGateway>
              {this.state.lightboxIsOpen ? (
                <Modal onClose={this.toggleLightbox}>
                  <LightBox
                    components={{
                      FooterCaption: () => <div>footer caption</div>
                    }}
                    currentIndex={this.state.selectedIndex}
                    // formatters={{ getAltText }}
                    frameProps={{ autoSize: "height" }}
                    views={this.state.awsApiData}
                  />
                </Modal>
              ) : null}
            </ModalGateway>
          </>
        )}
      </div>
    );
  }
}

export default Slider;

于 2020-04-15T04:21:52.790 回答