javascript - 在 reactjs 中使用分页创建组图像滑块
问题描述
我正在尝试创建一个带有分页的水平组图像滑块(类似于 react native 中的平面列表),但我在过去 2 天努力做到这一点。
我能够轻松创建 html/css,但问题是实现滑块的逻辑
我试图添加的功能:
5 个图像设置为活动图像,如果用户显示给用户
单击左侧按钮 ==> 前 5 个图像显示而不是活动图像
如果用户单击右键 ==> 显示下 5 个图像而不是活动图像
用户单击按钮时应更新分页(重要:每个索引代表一组图像......分页中的5个图像=> 1个索引)
我试图解决这个问题的方式:
1- 使用 createRef 为每个图像容器创建 ref
useEffect(() => {
const refsArray = [];
data.forEach((item, index) => {
//console.log(index);
const newRef = createRef();
refsArray.push(newRef);
});
setRefs(refsArray);
}, []);
2- 使用这种方法将主阵列分成 3 个不同的部分(上一个,下一个活动):
setPrevItems(refs.slice(startIndex - count, startIndex));
setActiveItems(refs.slice(startIndex, startIndex + count));
setNextItems(refs.slice(startIndex + count, startIndex + count + count));
3-逻辑添加到按钮
//left button handler
const handleLeftClick = () => {
//trying to hide the elments
activeItems.forEach((item, index) => {
item.current.style.left = `2000px`;
});
//trying to display previous items ... and using index to make them consecutive
prevItems.forEach((item, index) => {
item.current.style.left = `${300 * index}px`;
});
}
//right button handler
const handleRightClick = () => {
activeItems.forEach((item, index) => {
item.current.style.left = `-2000px`;
});
nextItems.forEach((item, index) => {
item.current.style.left = `${300 * index}px`;
});
}
附加节点:我在同一个项目中创建了一个轮播......我这样做没有问题,但是具体实现这个功能并不顺利。
感谢您提前提供任何建议:)
解决方案
查看react-slideshow-image 是一个幻灯片组件,React.js 支持滑动、淡出和缩放。您需要的大部分内容都可以通过此处描述的属性和方法来完成
const properties = {
autoPlay:false,
arrows: true,
}
/*SlideBanner represens each of your sectoins containing 5 images*/
const SlideBanner = (
<div style={{flexDirection:'row'}}>
<div className="imageContainer">
<img src="your img number 1"/>
</div>
<div className="imageContainer">
<img src="your img number 2"/>
</div>
...
<div className="imageContainer">
<img src="your img number 5"/>
</div>
</div>
)
<Slide {...properties}>
<SlideBanner/>
<SlideBanner/>
<SlideBanner/>
</Slide>
推荐阅读
- hibernate - Flyway & MariaDB:SQLException 外键约束的格式不正确
- variables - 如何将命令输出保存到变量
- algorithm - 为什么此代码通过 Prim 的算法生成错误的最小生成树?
- python - 如何忽略某些方法而不是其他方法的 __repr__()
- android - 在 Raspberry Pi 中将 android-BluetoothChat 连接到 bluez
- r - 将 Sys.time() 存储在数据框中
- javascript - React:它如何知道 DOM 组件何时已挂载,以便它可以调用 componentDidMount?
- r - 将指数分布拟合到任务持续时间计数
- javascript - 如何扩展/替换特定的图表类方法?
- ios - 创建一个提醒列表,它可以在您从我的应用创建的提醒应用中保存您的提醒