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

在移动上,当用户刷一张卡时,下一张卡应该自己来并居中,而在网络上,应该有一个左右键,当单击该按钮时显示相应的卡。
我搜索了一下,却找不到任何包裹。我如何实现这一点?我是新来的反应。
发布于 2019-04-21 17:09:00
作为其他答案,您有反应旋转组件-引导和反应带。
就我个人而言,我觉得反作用力比反应引导有更好的文档。你只需在他们的官方文档中找到一个旋转木马的例子。https://reactstrap.github.io/components/carousel/
但是,这些例子并没有你所期望的那样的滑动事件,我也找不到任何旋转木马与滑动事件,无论是网络和移动视图。所以,最后,我决定做一个自定义旋转木马与刷事件。
对于网络,我使用了onDragStart和onDragEnd。对于移动视图,我使用onTouchStart、onTouchMove和onTouchEnd来检测左、右滑动。
这里的变化。
//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>
);
}
}希望它能帮到你。
发布于 2019-04-20 18:55:24
正如在另一个答案中提到的,您可以找到已经构建的组件并使用它。
但是,您可以使用CSS滚动卡和flexbox自己实现它。
这里有一篇文章概述了这种方法(没有反应,但仍然适用)。
https://developers.google.com/web/updates/2018/07/css-scroll-snap
https://stackoverflow.com/questions/55655293
复制相似问题