ReactJS尝鲜:实现tab页切换和菜单栏切换和手风琴切换效果,进度条效果

在ReactJS中实现Tab页切换、菜单栏切换、手风琴切换效果和进度条效果的示例:

import React, { useState } from 'react';
import './App.css';

function App() {
  const [activeTab, setActiveTab] = useState(1);
  const [menuOpen, setMenuOpen] = useState(false);
  const [accordionOpen, setAccordionOpen] = useState(false);
  const [progress, setProgress] = useState(0);

  const tabs = [
    { id: 1, title: 'Tab 1' },
    { id: 2, title: 'Tab 2' },
    { id: 3, title: 'Tab 3' },
  ];

  const toggleMenu = () => {
    setMenuOpen(!menuOpen);
  };

  const toggleAccordion = () => {
    setAccordionOpen(!accordionOpen);
  };

  const updateProgress = () => {
    const interval = setInterval(() => {
      setProgress((prevProgress) => {
        if (prevProgress === 100) {
          clearInterval(interval);
          return 0;
        }
        return prevProgress + 10;
      });
    }, 1000);
  };

  return (
    <div className="App">
      <div className="tabs">
        {tabs.map((tab) => (
          <div
            key={tab.id}
            className={`tab ${activeTab === tab.id ? 'active' : ''}`}
            onClick={() => setActiveTab(tab.id)}
          >
            {tab.title}
          </div>
        ))}
      </div>
      <div className="menu">
        <button onClick={toggleMenu}>Toggle Menu</button>
        {menuOpen && <div className="menu-content">Menu Content</div>}
      </div>
      <div className="accordion">
        <button onClick={toggleAccordion}>Toggle Accordion</button>
        {accordionOpen && <div className="accordion-content">Accordion Content</div>}
      </div>
      <div className="progress">
        <button onClick={updateProgress}>Start Progress</button>
        <div className="progress-bar" style={{ width: `${progress}%` }}></div>
      </div>
    </div>
  );
}

export default App;

在这个示例中,我们使用React的状态钩子来管理不同效果的状态,并通过点击按钮来触发切换、进度条等操作。通过CSS样式来实现Tab切换、菜单栏、手风琴效果以及进度条效果的展示。