在 react js 中创建轮播卡片,就像在 fb messenger 中一样

Create carousel cards as in fb messenger in react js

我正在用 React js 开发一个像对讲小部件这样的聊天小部件,我想添加轮播卡片组件,就像在 Facebook Messenger 中一样。

在移动设备上,当用户刷一张卡时,下一张应该自动居中,而在网络上,应该有一个左右按钮,当点击时显示相应的卡。

我搜索过,但没有找到任何相关的软件包。 我该如何实施?我是新手。

react-bootstrap 有轮播组件

here 并且有 react-responsive-carousel here

正如另一个答案中提到的,您可以找到一个已经构建好的组件并使用它。

但是您可以使用 CSS scroll snap 和 flexbox 自己实现它。

这里有一篇文章概述了这种方法(不是反应但仍然适用)。

https://developers.google.com/web/updates/2018/07/css-scroll-snap

作为其他答案,你在 react-bootstrap 和 reactstrap 中有轮播组件。

就我个人而言,我觉得 reactstrap 的文档比 react-bootstrap 更好。您可以在他们的官方文档中简单地找到轮播的示例。 https://reactstrap.github.io/components/carousel/

但是,这些示例没有您预期的滑动事件,而且我也找不到任何带有用于 Web 和移动视图的滑动事件的旋转木马。所以最后,我决定用滑动事件制作一个自定义轮播。

对于网络,我使用了 onDragStartonDragEnd。对于移动视图,我使用 onTouchStartonTouchMoveonTouchEnd 来检测左右滑动。

修改如下。

//Global variables to hold the last and current X-axis positions.
var lastX = 0;
var currentX = 0;

//For web, detect swipe left and right based on mouse drag events. 
handleMouse = e => {
    e.persist();
    let type = e.type.toLowerCase();
    if (type === "dragstart") {
      lastX = e.clientX;
    } else {
      if (lastX === 0 || e.clientX === 0 || lastX === e.clientX) {
        return;
      }
      if (e.clientX > lastX) {
        this.previous();
        console.log("swife right");
      } else {
        this.next();
        console.log("swife left");
      }
    }
  };

  //For mobile, detect swipe left and right based on touch events. 
  handleTouch = e => {
    e.persist();
    let type = e.type.toLowerCase();

    if (type === "touchstart") {
      lastX = e.touches[0].clientX;
    }

    if (type === "touchmove") {
      currentX = e.touches[0].clientX;
    }

    if (type === "touchend") {
      if (lastX === 0 || currentX === 0 || lastX === currentX) {
        return;
      }
      if (currentX > lastX) {
        this.previous();
        console.log("swife right");
      } else {
        this.next();
        console.log("swife left");
      }
    }
  };

  //Modified render.
  render() {
    const { activeIndex } = this.state;
    const slides = items.map(item => {
      return (
        <CarouselItem
          onExiting={this.onExiting}
          onExited={this.onExited}
          key={item.src}
        >
          <img
            style={{ width: "100%" }}
            src={item.src}
            alt={item.altText}
            onTouchStart={e => this.handleTouch(e)}
            onTouchMove={e => this.handleTouch(e)}
            onTouchEnd={e => this.handleTouch(e)}
            onDragStart={e => this.handleMouse(e)}
            onDragEnd={e => this.handleMouse(e)}
          />
        </CarouselItem>
      );
    });

    return (
      <Carousel
        activeIndex={activeIndex}
        next={this.next}
        previous={this.previous}
        interval={false}
      >
        {slides}

        <CarouselControl
          direction="prev"
          directionText="Previous"
          onClickHandler={this.previous}
        />
        <CarouselControl
          direction="next"
          directionText="Next"
          onClickHandler={this.next}
        />
      </Carousel>
    );
   }
  }

工作demo and the fiddle

希望对您有所帮助。