首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建旋转木马卡,就像在fb信使中反应js一样

创建旋转木马卡,就像在fb信使中反应js一样
EN

Stack Overflow用户
提问于 2019-04-12 15:46:44
回答 3查看 2.1K关注 0票数 1

我正在开发一个聊天小部件,像react中的对讲机小部件,我想添加旋转木马卡组件,就像在facebook信使中一样。

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

我搜索了一下,却找不到任何包裹。我如何实现这一点?我是新来的反应。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-21 17:09:00

作为其他答案,您有反应旋转组件-引导和反应带。

就我个人而言,我觉得反作用力比反应引导有更好的文档。你只需在他们的官方文档中找到一个旋转木马的例子。https://reactstrap.github.io/components/carousel/

但是,这些例子并没有你所期望的那样的滑动事件,我也找不到任何旋转木马与滑动事件,无论是网络和移动视图。所以,最后,我决定做一个自定义旋转木马与刷事件。

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

这里的变化。

代码语言:javascript
复制
//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>
    );
   }
  }

工作演示小提琴

希望它能帮到你。

票数 5
EN

Stack Overflow用户

发布于 2019-04-20 18:55:24

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

但是,您可以使用CSS滚动卡和flexbox自己实现它。

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

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

票数 1
EN

Stack Overflow用户

发布于 2019-04-20 16:06:29

react引导程序有一个旋转木马组件。

这里和有反应反应的旋转木马这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55655293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档