正如 Changoon Lee在他们的回答中提到的那样,您可以使用每次滑动前后都会调用的beforeChange
和回调。afterChange
如果您只想检测按钮点击(而不是键盘滑动或拖动),您可以创建新的按钮组件并将它们作为自定义箭头传递。您的自定义按钮将收到道具/状态列表;其中之一是react-multi-carousel
'onClick
处理程序。
您可以将自定义按钮作为道具(customLeftArrow
和customRightArrow
)传递给轮播。
function CustomRightArrow({ onClick }) {
function handleClick() {
// do whatever you want on the right button click
console.log('Right button clicked, go to next slide');
// ... and don't forget to call onClick to slide
onClick();
}
return (
<button
onClick={handleClick}
aria-label="Go to next slide"
className="react-multiple-carousel__arrow react-multiple-carousel__arrow--right"
/>
);
}
function App() {
return (
<Carousel
customLeftArrow={<CustomLeftArrow />}
customRightArrow={<CustomRightArrow />}
infinite
keyBoardControl
>
{images.map((image, i) => {
return (
<img
key={i}
style={{ width: '100%', height: '100%' }}
src={image}
alt=""
/>
);
})}
</Carousel>
);
}