效果类似antd的Drawer,点击灰色遮罩层可以平滑关闭。
import React, { useState } from 'react';
import { Button } from 'antd';
import styles from './test.less';
export default function() {
const [v, setV] = useState(false);
const [mask, setMask] = useState(false);
return (
<>
<Button onClick={() => setV(!v)}>click me</Button>
<div className={styles.container}>
<aside
className={styles.mask}
style={{ display: v || mask ? 'flex' : 'none' }}
onClick={() => setV(false)}
onAnimationEnd={a => {
if (a.animationName === styles.myMoveLeftOut) setMask(false);
else if (a.animationName === styles.myMoveLeftIn) setMask(true);
}}
>
<div
className={v ? `${styles.menu} ${styles.myenter}` : `${styles.menu} ${styles.myleave}`}
>
menu....
</div>
</aside>
</div>
</>
);
}
样式:
.container {
background-color: antiquewhite;
height: 80vh;
transition: all 0.3s;
.mask {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.2);
z-index: 9999;
.menu {
width: 120px;
background-color: green;
}
.myenter {
animation: myMoveLeftIn 1s;
}
.myleave {
animation: myMoveLeftOut 1s;
}
}
@keyframes myMoveLeftIn {
0% {
transform: translateX(-100%);
transform-origin: 0 0;
opacity: 0;
}
100% {
transform: translateX(0%);
transform-origin: 0 0;
opacity: 1;
}
}
@keyframes myMoveLeftOut {
0% {
transform: translateX(0%);
transform-origin: 0 0;
opacity: 1;
}
100% {
transform: translateX(-100%);
transform-origin: 0 0;
opacity: 0;
}
}
}