antd tab和Carousel实现轮播

朱雀 2022-12-22 00:55 339阅读 0赞

1、tab通过定时器实现自动轮播

2、通过Carousel组件实现自动轮播

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3ODE1NTk2_size_16_color_FFFFFF_t_70

完整代码

  1. import React , { Component } from 'react'
  2. import "antd/dist/antd.css";
  3. import { Tabs , Carousel } from 'antd'
  4. const contentStyle = {
  5. height: '160px',
  6. color: '#fff',
  7. lineHeight: '160px',
  8. textAlign: 'center',
  9. background: '#364d79',
  10. };
  11. class App extends Component {
  12. constructor(props){
  13. super(props);
  14. this.state={
  15. activeKey:"tabOne"
  16. }
  17. }
  18. componentDidMount(){
  19. this.tabs = document.querySelectorAll('.work-order .ant-tabs-tab')
  20. this.tabTimer = setInterval(() => {
  21. this.handle(this.state.activeKey)
  22. }, 3000)
  23. }
  24. handle(value){
  25. // console.log("value",value)
  26. switch (value) {
  27. case "tabOne":
  28. this.tabs[1].click()
  29. break
  30. case "tabTwo":
  31. this.tabs[2].click()
  32. break
  33. case "tabThree":
  34. this.tabs[0].click()
  35. break
  36. default:
  37. break
  38. }
  39. }
  40. handleTabChange = key => {
  41. clearInterval(this.tabTimer)
  42. // console.log("key",key)
  43. setTimeout(()=>{
  44. if (key === 'tabOne') {
  45. this.setState({activeKey:"tabOne"})
  46. }
  47. else if (key === 'tabTwo') {
  48. this.setState({activeKey:"tabTwo"})
  49. }
  50. else if (key === 'tabThree') {
  51. this.setState({activeKey:"tabThree"})
  52. }
  53. },100)
  54. this.tabTimer = setInterval(() => {
  55. this.handle(this.state.activeKey)
  56. }, 3000)
  57. }
  58. render() {
  59. const {TabPane} = Tabs
  60. return(
  61. <div className="work-order">
  62. {/* 通过Carousel组件实现自动轮播 */}
  63. <Carousel autoplay autoplaySpeed={3000}>
  64. <div>
  65. <h3 style={contentStyle}>1</h3>
  66. </div>
  67. <div>
  68. <h3 style={contentStyle}>2</h3>
  69. </div>
  70. <div>
  71. <h3 style={contentStyle}>3</h3>
  72. </div>
  73. <div>
  74. <h3 style={contentStyle}>4</h3>
  75. </div>
  76. </Carousel>
  77. {/* 通过定时器实现自动轮播 */}
  78. <Tabs onChange={this.handleTabChange} >
  79. <TabPane tab="Tab 1" key="tabOne">
  80. Content of Tab Pane 1
  81. </TabPane>
  82. <TabPane tab="Tab 2" key="tabTwo">
  83. Content of Tab Pane 2
  84. </TabPane>
  85. <TabPane tab="Tab 3" key="tabThree">
  86. Content of Tab Pane 3
  87. </TabPane>
  88. </Tabs>
  89. </div>
  90. )
  91. }
  92. }
  93. export default App

发表评论

表情:
评论列表 (有 0 条评论,339人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vue-carousel

    单页面开发中,经常需要用到轮播展示相关信息,那么vue中该怎么使用轮播图呢?这次小编给大家介绍在vue中使用vue-carousel做轮播。 首先安装vue-carouse