让子元素只在父元素过渡结束后渲染?
Make children elements render only after parent element transition is over?
我有一个导航栏,当扩展过渡使用 50% 的屏幕时,在这个扩展的导航元素内部,是一个搜索字段组件。我面临的问题是搜索字段在导航栏完成扩展之前出现(我知道这是有道理的)。我只是不知道如何解决这个问题。提前致谢!这是相关代码:
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:
.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;
}
使用附加到包装器的 'transitionend' 事件侦听器可能是个好主意。示例: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>
我有一个导航栏,当扩展过渡使用 50% 的屏幕时,在这个扩展的导航元素内部,是一个搜索字段组件。我面临的问题是搜索字段在导航栏完成扩展之前出现(我知道这是有道理的)。我只是不知道如何解决这个问题。提前致谢!这是相关代码:
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:
.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;
}
使用附加到包装器的 'transitionend' 事件侦听器可能是个好主意。示例: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>