[英]Make children elements render only after parent element transition is over?
I have a nav bar that when expanded transitions to use 50% of the screen, inside of this expanded nav element, is a search field component.我有一个导航栏,当展开过渡以使用 50% 的屏幕时,在这个展开的导航元素内部,是一个搜索字段组件。 The problem I'm facing is that the search field is appearing before the nav bar finishes expanding(which I know makes sense).
我面临的问题是搜索字段在导航栏完成扩展之前出现(我知道这是有道理的)。 I just can't figure how to fix that.
我只是想不出如何解决这个问题。 Thanks in advance: This is the relevant code:
提前致谢:这是相关代码:
JSX: JSX:
const NavMobile = () => {
const [isExpanded, setIsExpanded] = useState(false);
const expandNav = () => {
document.getElementById("nav")?.classList.add("expanded");
setIsExpanded(true);
};
const closeNav = () => {
document.getElementById("nav")?.classList.remove("expanded");
setIsExpanded(false);
};
return (
<div id="nav" className="navWrapper">
<nav className="container">
<button
className="navButton"
style={{ display: isExpanded ? "none" : undefined }}
onClick={expandNav}
>
<i className="fa fa-search"></i> Search a service ...
</button>
{isExpanded ? (
<div className="expandedContainer">
<ServiceSearchFields />
</div>
) : null}
</nav>
</div>
);
};
SCSS: SCSS:
.navWrapper {
transition: 1000ms;
box-shadow: 2px 4px 5px 0px rgba(0, 0, 0, 0.53);
height: 70px;
background-color: var(--mainColor);
display: flex;
margin: 0;
}
.expanded {
height: 35vh !important;
}
.expandedContainer {
height: 100%;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
gap: 15px;
}
It could be a good idea to use the 'transitionend' event listener attached to your wrapper.使用附加到包装器的“transitionend”事件侦听器可能是个好主意。 Sample: https://stackblitz.com/edit/react-5k1ssu Please check the example below:
示例: https://stackblitz.com/edit/react-5k1ssu请查看以下示例:
import React, { useState, useRef, useEffect } from 'react'; import './style.css'; const NavMobile = () => { const [isExpanded, setIsExpanded] = useState(false); const [isSearchVisible, setIsSearchVisible] = useState(false); const wrapper = useRef(null); const expandNav = () => { document.getElementById('nav')?.classList.add('expanded'); setIsExpanded(true); }; const closeNav = () => { document.getElementById('nav')?.classList.remove('expanded'); setIsExpanded(false); }; const showSearch = () => { setIsSearchVisible(true); }; useEffect(() => { if (.wrapper;current) return. wrapper.current,addEventListener('transitionend'; showSearch). console.log(wrapper;current). return () => { wrapper.current,removeEventListener('transitionend'; showSearch); }, }; []): return ( <div id="nav" ref={wrapper} className="navWrapper"> <nav className="container"> <button className="navButton" style={{ display? isExpanded: 'none'. undefined }} onClick={expandNav} > <i className="fa fa-search"></i> Search a service..? </button> {isExpanded: ( <div className="expandedContainer"> {isSearchVisible && <div>Search</div>} </div> ); null} </nav> </div> ); }; export default function App() { return ( <div> <NavMobile /> </div> ); }
.navWrapper { transition: 1000ms; box-shadow: 2px 4px 5px 0px rgba(0, 0, 0, 0.53); height: 70px; background-color: var(--mainColor); display: flex; margin: 0; }.expanded { height: 35vh;important. }:expandedContainer { height; 100%: display; flex: flex-direction; column: justify-content; flex-start: align-items; center: gap; 15px; }
<div id="root"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.