reactjs - 确定具有多个 swiper 实例的索引
问题描述
我正在使用 swiperjs 在我的页面上显示数据行,这是一个反应应用程序。每行都是一个 swiper 实例。如果我在特定行上刷卡,我如何知道我目前处于哪个刷卡器索引?有没有办法找出滑动开始于特定行的时间?
constructor(props) {
super(props);
}
componentDidMount() {
this.buildSwiper();
}
// Initialize the swiper component.
buildSwiper() {
this.swiper = new Swiper('.swiper-container', {
slidesPerView: auto,
freeMode: true,
freeModeSticky: true,
simulateTouch: true,
on: {
reachEnd: this.onReachEnd
},
});
}
onReachEnd() {
// **Need help here: I want to append the slide only on the correct row I am swiping at.**
this.swiper.forEach(swiper =>
swiper.appendSlide('<div class="swiper-slide">Slide 10</div>')
)
}
renderDepartments(dept) {
<div className="swiper-container">
<div className="swiper-wrapper">
<div className="swiper-slide" key={dept.id}>
{dept.name}
</div>
</div>
</div>
}
render() {
const { departments } = this.props;
return (
<div>{departments && departments.map(dept => this.renderDepartments(dept))}</div>
);
}
};
export default connect(mapStateToProps, mapDispatchToProps)(injectSheet(styles)(Department));
解决方案
基于 swiperjs API 文档,reachEnd
事件接受swiper
实例作为参数。那是您当前正在访问的当前刷卡器。
onReachEnd(swiper) {
swiper.appendSlide('<div class="swiper-slide">Slide 10</div>')
}
推荐阅读
- javascript - 标记 event.listener 迭代问题,Laravel
- node.js - node.js 中的 Google-Cloud 模块安装失败
- python - 基于 Python 的动态值替换程序
- c# - 什么是 Unity 等价于 StructureMap 的 For
().Use(某事) - c++ - Why does pass-by-value parameters often make code optimization easier for the compiler?
- c++ - 编译器如何区分前缀和后缀(递增和递减)运算符重载与 C++ 中的虚拟参数
- ruby-on-rails - Am I stubbing data correctly?
- python - 为什么这个python代码不能执行?
- maxima - 如何告诉 Maxima 大型表达式的子表达式的有效近似值?
- javascript - 如何使模态停止或继续 onClick 事件