我在NextJS中创建了一个手风琴组件.现在,我想自动逐个打开手风琴项目,并 for each 手风琴项目设定自定义持续时间.此外,手风琴组旁边的图像应根据打开的手风琴项更改.

For example: https://www.perspective.co/(大约是本页面的第4部分)

Current Work Accordion Group

这是我的代码:

Accordion Item

'use client'

const OnboardItem = ({ title, description, duration, isOpen, toggle }:{ title:string, description:string, duration:number, isOpen:boolean, toggle:any }) => {

  return(
    <div className='relative'>
      <div className='w-0.5 bg-neutral-300 dark:bg-neutral-500 absolute left-0 top-0 bottom-0'></div>
      <div className='ps-5 items-center'>
        <p className={`text-2xl leading-[38px] select-none cursor-pointer ${isOpen ? 'text-black dark:text-primary' : 'text-neutral-400 dark:text-neutral-300'}`} onClick={toggle}>{title}</p>
        <div className={`grid mt-1 overflow-hidden ease-in-out ${isOpen ? 'accordion-animate-open pb-1' : 'accordion-animate'}`}>
          <div className='leading-7 min-h-0'><p className='select-none'>{description}</p></div>
        </div>
      </div>
    </div>
  )
}

export default OnboardItem

Accordion Group

const [isOpen, setIsOpen] = useState(0)
    
const toggleOpen = (id:any) => () => setIsOpen(id)
    
  onboardData.forEach((element, index) => {
        
 });
    
 { 
   onboardData.map(({title, description, duration}, index) => (
     <OnboardItem 
       key={index}
       title={title}
       description={description}
       duration={duration}
       isOpen={isOpen === index}
       toggle={toggleOpen(index)}
     />
   ))
 }

Data

export const onboardData = [
  {
    title: 'Discovery Call',
    description: 'How embark on a collaborative project, simply visit our website and access our contact form to get in touch where we collaborate with you to outline the project scope and determine how we can assist you with our services.',
    duration: 5000
  },
  {
    title: 'Prototype Creation',
    description: 'What embark on a collaborative project, simply visit our website and access our contact form to get in touch where we collaborate with you to outline the project scope and determine how we can assist you with our services.',
    duration: 15000
  },
  {
    title: 'Design & Development',
    description: 'How embark on a collaborative project, simply visit our website and access our contact form to get in touch where we collaborate with you to outline the project scope and determine how we can assist you with our services.',
    duration: 10000
  },
  {
    title: 'Testing Modules',
    description: 'Where embark on a collaborative project, simply visit our website and access our contact form to get in touch where we collaborate with you to outline the project scope and determine how we can assist you with our services.',
    duration: 5000
  },
  {
    title: 'Launch',
    description: 'Can embark on a collaborative project, simply visit our website and access our contact form to get in touch where we collaborate with you to outline the project scope and determine how we can assist you with our services.',
    duration: 10000
  },
];

推荐答案

Accordion Group组件中添加useEffect,并确保在该组件的顶部使用"use client".

  useEffect(() => {
    const intervalId = setInterval(() => {
      setIsOpen((isOpen + 1) % onboardData.length);
    }, onboardData[isOpen].duration);
    return () => clearInterval(intervalId);
  }, [isOpen]);

Javascript相关问答推荐

当没有固定间隔时,是否可以在d3.js中进行画笔捕捉?

try 在addEventHandler内设置表单的文件输入.值=空

Flutter:显示PDF和视频,但阻止下载

为什么JavaScript双边字符串文字插值不是二次的?

如何使用Echart 5.5.0创建箱形图

Bootstrap动态选项卡在切换选项卡后保持活动状态,导致元素堆叠

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

如何使用JavaScript将文本插入空div

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

从Node JS将对象数组中的数据插入Postgres表

查找最长的子序列-无法重置数组

在react JS中映射数组对象的嵌套数据

如何使用子字符串在数组中搜索重复项

从Nextjs中的 Select 项收集值,但当单击以处理时,未发生任何情况

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

如何使用TypeScrip设置唯一属性?

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

使用自动识别发出信号(&Q)

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件

重新呈现-react -筛选数据过多