Jer*_*ews 1 chat carousel reactjs
我正在开发React js中的对讲机小部件之类的聊天小部件,并且我想添加旋转木马卡组件,就像在Facebook Messenger中一样。
在移动设备上,当用户滑动一张卡时,下一张卡应居中并居中;在网络上,应该有一个左右按钮,单击该按钮可显示相应的卡。
我搜索了此文件,但找不到任何软件包。我该如何实施?是新来的反应。
作为其他答案,您在react-bootstrap和reactstrap中具有轮播组件。
我个人觉得Reactstrap比react-bootstrap有更好的文档。您可以在他们的官方文档中找到轮播的示例。 https://reactstrap.github.io/components/carousel/
但是,这些示例没有您期望的滑动事件,而且我也找不到针对Web和移动视图具有滑动事件的任何轮播。所以最后,我决定通过滑动事件制作自定义轮播。
对于网络,我已经使用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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
希望对您有帮助。
| 归档时间: |
|
| 查看次数: |
419 次 |
| 最近记录: |