antd tab和Carousel实现轮播
程序员文章站
2024-03-23 15:20:52
...
1、tab通过定时器实现自动轮播
2、通过Carousel组件实现自动轮播
完整代码
import React , { Component } from 'react'
import "antd/dist/antd.css";
import { Tabs , Carousel } from 'antd'
const contentStyle = {
height: '160px',
color: '#fff',
lineHeight: '160px',
textAlign: 'center',
background: '#364d79',
};
class App extends Component {
constructor(props){
super(props);
this.state={
activeKey:"tabOne"
}
}
componentDidMount(){
this.tabs = document.querySelectorAll('.work-order .ant-tabs-tab')
this.tabTimer = setInterval(() => {
this.handle(this.state.activeKey)
}, 3000)
}
handle(value){
// console.log("value",value)
switch (value) {
case "tabOne":
this.tabs[1].click()
break
case "tabTwo":
this.tabs[2].click()
break
case "tabThree":
this.tabs[0].click()
break
default:
break
}
}
handleTabChange = key => {
clearInterval(this.tabTimer)
// console.log("key",key)
setTimeout(()=>{
if (key === 'tabOne') {
this.setState({activeKey:"tabOne"})
}
else if (key === 'tabTwo') {
this.setState({activeKey:"tabTwo"})
}
else if (key === 'tabThree') {
this.setState({activeKey:"tabThree"})
}
},100)
this.tabTimer = setInterval(() => {
this.handle(this.state.activeKey)
}, 3000)
}
render() {
const {TabPane} = Tabs
return(
<div className="work-order">
{/* 通过Carousel组件实现自动轮播 */}
<Carousel autoplay autoplaySpeed={3000}>
<div>
<h3 style={contentStyle}>1</h3>
</div>
<div>
<h3 style={contentStyle}>2</h3>
</div>
<div>
<h3 style={contentStyle}>3</h3>
</div>
<div>
<h3 style={contentStyle}>4</h3>
</div>
</Carousel>
{/* 通过定时器实现自动轮播 */}
<Tabs onChange={this.handleTabChange} >
<TabPane tab="Tab 1" key="tabOne">
Content of Tab Pane 1
</TabPane>
<TabPane tab="Tab 2" key="tabTwo">
Content of Tab Pane 2
</TabPane>
<TabPane tab="Tab 3" key="tabThree">
Content of Tab Pane 3
</TabPane>
</Tabs>
</div>
)
}
}
export default App
推荐阅读
-
antd tab和Carousel实现轮播
-
Android中用RxJava和ViewPager实现轮播图
-
Android中用RxJava和ViewPager实现轮播图
-
Android 中使用 ViewPager实现屏幕页面切换和页面轮播效果
-
Android 中使用 ViewPager实现屏幕页面切换和页面轮播效果
-
Android 微信6.1 tab栏图标和字体颜色渐变的实现
-
Android 微信6.1 tab栏图标和字体颜色渐变的实现
-
android ViewPager实现自动无限轮播和下方向导圆点
-
android ViewPager实现自动无限轮播和下方向导圆点
-
Android中TabLayout+ViewPager实现tab和页面联动效果