javascript - onClick 后元素上的 CSS 动画
问题描述
我想用 onclick 为我的按钮设置动画,但它不起作用:
css 文件(style.css):
.slide-out-top {
-webkit-animation: slide-out-top 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
animation: slide-out-top 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
}
@-webkit-keyframes slide-out-top {
0% {
-webkit-transform: translateY(0);
transform: translateY(0);
opacity: 1;
}
100% {
-webkit-transform: translateY(-1000px);
transform: translateY(-1000px);
opacity: 0;
}
}
@keyframes slide-out-top {
0% {
-webkit-transform: translateY(0);
transform: translateY(0);
opacity: 1;
}
100% {
-webkit-transform: translateY(-1000px);
transform: translateY(-1000px);
opacity: 0;
}
}
.slide-out-bottom {
-webkit-animation: slide-out-bottom 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
animation: slide-out-bottom 1s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
-webkit-animation-timing-function: ease;
animation-timing-function: ease;
}
@-webkit-keyframes slide-out-bottom {
0% {
-webkit-transform: translateY(0);
transform: translateY(0);
opacity: 1;
}
100% {
-webkit-transform: translateY(1000px);
transform: translateY(1000px);
opacity: 0;
}
}
@keyframes slide-out-bottom {
0% {
-webkit-transform: translateY(0);
transform: translateY(0);
opacity: 1;
}
100% {
-webkit-transform: translateY(1000px);
transform: translateY(1000px);
opacity: 0;
}
}
jsx 文件(preact):
import { h, Component } from 'preact';
import "../../style/index.css";
import "../../style/bootstrap.min.css";
import "./style.css";
export default class Home extends Component {
buttonExit = () => {
document.getElementById('buttonToExit').className = 'btn btn-outline-light slide-out-bottom';
document.getElementById('textToExit').className = 'pt-5 pb-3 font-weight-bold text-light h1 slide-out-top';
}
render = () => {
return (
<div className="App vh-100">
<div className="h-70 container">
<div className="py-5"> </div>
<div className="py-5" style={{ width: "100%" }}>
<div class="container">
<div class="row">
<div class="col-sm"> </div>
<div class="col text-center">
<div id="textToExit" className="pt-5 pb-3 font-weight-bold text-light h1">Accès en ligne</div>
<button type="button" id="buttonToExit" onClick={this.buttonExit} className="btn btn-outline-light">Se Connecter</button>
</div>
<div class="col-sm"> </div>
</div>
</div>
</div>
</div>
</div>
);
}
}
与style.css
js 文件位于同一文件夹中。
我的函数应该修改按钮和文本元素的“className”。经过几次测试,该函数似乎被很好地调用了,并且通常动画是正常的。
有人可以帮助我吗?
解决方案
className
在 DOM 中不存在,它是一个 JSX 抽象,而不是class
因为这是一个保留的 JavaScript 字。当您在 React 中使用虚拟 DOM(或 preact,我假设,并不完全熟悉它)时,所有文档语句(例如)getElementById
也不会按预期运行。
我会在这里考虑两个选项,使用Refs
or State
。使用Refs
,您可以跟踪 DOM 节点并直接更新它,尽管这不是一个动态或一般可取的解决方案,因为您必须Refs
事先声明所有节点。但是,嘿,它有效。
textRef = createRef();
buttonRef = createRef();
buttonExit = () => {
textRef.current.classList.add('slide-out-top');
buttonRef.current.classList.add('slide-out-bottom');
}
render = () => {
return (
...your code...
<div id="textToExit" ref={this.textRef}...>
<button type="button" id="buttonToExit" ref={this.buttonRef}...>
...your code...
)
}
使用State
,您可以根据组件的当前状态有选择地激活类列表的不同部分。
state = {
exit: false
}
buttonExit = () => {
this.setState({exit: true});
}
render = () => {
const buttonExit = this.state.exit ? 'slide-out-bottom' : '';
const textExit = this.state.exit ? 'slide-out-top' : '';
return (
...your code...
<div id="textToExit" className={`pt-5 pb-3 font-weight-bold text-light h1 ${textExit}`}...>
<button type="button" id="buttonToExit" className={`btn btn-outline-light ${buttonExit}`}...>
...your code...
)
}
推荐阅读
- reactjs - useTranslation 在反应钩子中不起作用?
- c# - 如何在 .Net Core 和 Angular 中升级“Abp-zero-Template”
- process - NSIS 按全局关闭进程
- c++ - 如何移动 QGraphicsView 的内容?
- android - 仅在选中状态下更改复选框颜色并禁用用户交互
- ruby - 循环数组并将值存储到ruby中的哈希值
- sockets - 有时,asio::tcp::socket 对象会在调用 shutdown/close 之前自动关闭
- apache-kafka - Apache Kafka - 没有密钥的消息
- firebase - 是否可以获得按电子邮件或上次登录排序的方法 listAllUsers(NextPageToken) 的结果?
- angular - Angular:类型“周期性背景同步”不可分配给类型“PermissionName”