我正在尝试创建一个像 Bootstrap https://getbootstrap.com/docs/4.3/components/collapse/ 中的那样的 Accordion 菜单
我已经设法让它打开和关闭得很好,但我缺少平滑的过渡:/
这就像转换没有被应用。
import React, { useState } from 'react';
import styled from 'styled-components';
import { Button } from './common/button';
const AccordionWrapper = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
background-color: var(--Secondary-color-dark);
border-radius: 10px;
height: auto;
padding: 2%;
text-align: center;
transition: all 0.6s ease-in-out;
`;
const InternalWrapper = styled.div`
width: 100%;
max-height: ${(props) => (props.open ? '100%' : '0')};
transition: all 1s ease-in-out;
overflow: hidden;
`;
const Accordion = ({ title, subTitle, btnText }) => {
const [ open, setOpen ] = useState(false);
const handleClick = () => {
setOpen(!open);
};
return (
<AccordionWrapper>
<h2>{title}</h2>
<h3>{subTitle}</h3>
<InternalWrapper open={open}>
<h1>Hello</h1>
</InternalWrapper>
<Button padding="5px" onClick={handleClick}>
{btnText}
</Button>
</AccordionWrapper>
);
};
Accordion.defaultProps = {
title : 'title',
subTitle : 'subtitle',
btnText : 'Read more >>'
};
export default Accordion;
这是一个代码笔复制品。 https://codepen.io/hichihachi/pen/MWwKZEO?editors=0010
如有任何帮助,我们将不胜感激,谢谢。
最佳答案
当您以百分比和其他一些单位设置最大高度过渡时,它不起作用。为了使转换工作,您可以定义类似的内容
最大高度:${(props) => (props.open ? '100px' : '0')};
https://codepen.io/alonabas/pen/PoqNYLR?editors=1111
但是如果您的内容高度超过 100 像素,当您打开 Accordion 时,内容将被剪切。在这种情况下,您可以使用 jQuery 来计算内容的确切大小或使用 max-height 的一些最大可能值。
这两个选项的描述如下: https://stackoverflow.com/a/8331169/2916925
关于javascript - 如何使用 react 和样式组件制作 Accordion 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60229046/