gpt4 book ai didi

javascript - 在按键上添加背景颜色并淡出 javascript

转载 作者:行者123 更新时间:2023-11-28 06:37:14 25 4
gpt4 key购买 nike

在按键回车时,我想添加一种红色,它会立即淡出以创建闪烁效果。

我尝试在 KeyPress 上添加一个新类,将不透明度转换为 0:

function enterpressalert(e, text) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
document.getElementById('body').className = "show";
}
}
    #body {
background-color: rgb(175, 30, 30);
opacity: .25;
transition: opacity 0.4s ease-in;
-ms-transition: opacity 0.4s ease-in;
-moz-transition: opacity 0.4s ease-in;
-webkit-transition: opacity 0.4s ease-in;
}

#body.show {
opacity: 0;
transition: opacity 0.4s ease-out;
-ms-transition: opacity 0.4s ease-out;
-moz-transition: opacity 0.4s ease-out;
-webkit-transition: opacity 0.4s ease-out;
}
<body id="body" onKeyPress="enterpressalert(event, this)></body>

最佳答案

这里:

function enterpressalert(event, text) {
var code = event.keyCode || event.which;
if (code == 13) {
document.getElementById('body').style.animationName = "alert";
setTimeout(function() {
document.getElementById('body').id = "bodyB";
setTimeout(function() {
document.getElementById('bodyB').id = "body";
document.getElementById('body').style.animationName = "nothing";
}, 4000);
}, 100);
}
}
    #body {
background-color: rgb(175, 30, 30);
opacity: .25;
animation-name: nothing;
animation-duration: 0.1s;
}

#bodyB {
background-color: yellow;
opacity: .25;
animation-name: phase2;
animation-duration: 4s;
}

@keyframes alert {
from {background-color: yellow}
to {background-color: rgb(175, 30, 30);;}
}

@keyframes phase2 {
from {background-color: yellow;}
to {background-color: rgb(175, 30, 30);}
}
<body id="body" onkeypress="enterpressalert(event, 'TEXT?')">

</body>

关于javascript - 在按键上添加背景颜色并淡出 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34563985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com