【发布时间】:2021-10-25 23:54:21
【问题描述】:
当我使用 JavaScript 中的 classList.toggle 属性更改表单的输入类和标签时,我一直在尝试使用 CSS 添加淡入淡出动画。我不明白为什么我的代码不起作用,它正确更改了类但动画没有发生;是不是我做错了什么?
const checkBox = document.querySelector('#checkBox');
const label = document.querySelector('#label');
function verify() {
if (checkBox.checked) {
label.innerHTML = '<-<span>Un</span>Checked :)'
}
if (checkBox.checked == false) {
label.innerHTML = '<-UnChecked :('
}
}
checkBox.addEventListener('click', verify);
checkBox.click();
setInterval(() => {
checkBox.click();
label.classList.toggle('animation');
checkBox.classList.toggle('animation');
label.classList.toggle('animation2');
checkBox.classList.toggle('animation2');
setTimeout(() => {
checkBox.click()
label.classList.toggle('animation');
checkBox.classList.toggle('animation');
label.classList.toggle('animation2');
checkBox.classList.toggle('animation2');
}, 2000);
}, 4000);
* {
font-family: 'Cartograph CF';
}
#checkBox {
height: 20px;
width: 20px;
margin-right: 10px;
}
form {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 50px;
font-size: 30px;
}
label {
user-select: none;
}
.animation {
animation: fade .3s ease-in-out forwards;
opacity: 0;
}
.animation2 {
animation: fade .3s ease-in-out forwards;
opacity: 0;
}
span {
opacity: 0;
}
@keyframes fade {
form {
opacity: 0;
}
to {
opacity: 1;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form>
<input type="checkbox" id="checkBox" class="animation">
<label for="checkBox" id="label" class="animation"><-UnChecked :(</label>
</form>
</body>
</html>
【问题讨论】:
-
css 中的错字
form->from
标签: javascript html css dom css-animations