【发布时间】:2021-09-11 02:05:51
【问题描述】:
我用 react 开始了一个简单的项目。在我的项目中,我有一个段落,当鼠标悬停在段落上时(鼠标进入事件),段落下方会出现一个正方形,当从段落悬停时(鼠标离开事件)该正方形消失。但这发生得太快了,所以我想顺利地改变它,我想使用不透明度并将其从 0 更改为 1,并在我的事件发生时反转。但我不知道如何通过反应动画来改变不透明度。
这是我的应用程序
import './index.css';
import React, {useState} from "react";
function App() {
const [isShowSquare, setIsShowSquare] = useState(false);
const showSquare = () => {
setIsShowSquare(true);
}
const hideSquare = () => {
setIsShowSquare(false);
}
return (
<div>
<p onMouseEnter={showSquare} onMouseLeave={hideSquare} style={{display:'inline-block'}}>Hover Me</p>
{isShowSquare ?
<div className='square'>
</div>
: null
}
</div>
);
}
export default App;
这是我的 index.css
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.square{
width: 50px;
height: 50px;
background-color: #61dafb;
}
如果有人可以帮助我,我将非常感激
【问题讨论】:
-
你检查答案了吗?
标签: javascript css reactjs typescript mouseevent