antD 走马灯跳到指定页面
时间: 2023-10-04 07:11:15 浏览: 155
Ant Design的走马灯组件(Carousel)可以通过`afterChange`事件来实现跳转到指定页面。
首先,在`Carousel`组件上添加`afterChange`事件监听器:
```jsx
import { Carousel } from 'antd';
function MyCarousel() {
const handleAfterChange = (current) => {
console.log(`Current slide index: ${current}`);
// 跳转到指定页面的代码
};
return (
<Carousel afterChange={handleAfterChange}>
{/* Carousel 的子组件 */}
</Carousel>
);
}
```
在事件处理函数`handleAfterChange`中,可以获取到当前轮播图所在的索引`current`。接下来,可以在此基础上编写跳转到指定页面的代码。
例如,如果每张轮播图都对应一个链接地址,那么可以将链接地址存储在数组中,然后根据`current`索引获取对应的链接地址,并使用`window.location.href`跳转:
```jsx
import { Carousel } from 'antd';
function MyCarousel() {
const links = ['https://www.example.com/page1', 'https://www.example.com/page2', 'https://www.example.com/page3'];
const handleAfterChange = (current) => {
console.log(`Current slide index: ${current}`);
window.location.href = links[current];
};
return (
<Carousel afterChange={handleAfterChange}>
{/* Carousel 的子组件 */}
</Carousel>
);
}
```
当然,也可以根据业务需求自定义跳转逻辑。
阅读全文
相关推荐
















