当我使用classList.toggle
JavaScript 中的属性更改表单的输入和标签的类时,我一直在尝试使用 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 transition,动画属性更难调整。平滑更改文本的解决方案,在标签内创建两个元素并交替显示。
const checkBox = document.querySelector('#checkBox');
const visible = document.querySelector('.visible');
const hidden = document.querySelector('.hidden');
function verify() {
if (checkBox.checked) {
checkBox.classList.remove('animation');
visible.classList.add('animation');
hidden.classList.remove('animation');
}
if (!checkBox.checked) {
checkBox.classList.add('animation');
visible.classList.remove('animation');
hidden.classList.add('animation');
}
}
checkBox.addEventListener('click', verify);
checkBox.click();
setInterval(() => {
checkBox.checked = false;
checkBox.classList.remove('animation');
visible.classList.remove('animation');
hidden.classList.add('animation');
setTimeout(() => {
checkBox.checked = true;
checkBox.classList.add('animation');
visible.classList.add('animation');
hidden.classList.remove('animation');
}, 2000);
}, 4000);
* {
font-family: 'Cartograph CF';
}
#checkBox {
height: 20px;
width: 20px;
margin-right: 10px;
opacity: 1;
transition: opacity 0.5s ease-in-out;
}
form {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 50px;
font-size: 30px;
}
label {
width: 250px;
height: 30px;
user-select: none;
position: relative;
}
.animation {
opacity: 0;
}
span {
position: absolute;
opacity: 1;
transition: opacity 0.5s ease-in-out;
}
<form>
<input type="checkbox" id="checkBox" class="animation" />
<label for="checkBox" id="label"><span class="visible"><-UnChecked :(</span>
<span class="hidden animation"><- Checked :)</span></label
>
</form>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句