javascript - jquery 检查事件和 css 动画

标签 javascript jquery html css

我制作了一个 css 动画并使用 jquery 触发了复选框事件。但这有时不起作用。我不明白我做错了什么。有人知道我做错了什么吗?

预期 - 如果输入被选中或未被选中(任何检查事件),总是动画文本

得到 - 有时有效,有时无效,例如多次点击

代码笔 https://codepen.io/billyjov/pen/GxzOLZ

代码

$(function() {

    $(".animated").addClass("grow-one");
    
    var $checkbox = $(".checkbox");
    var $animatedText = $("#animatedText")
  $checkbox.change(function () {
		if ($animatedText.hasClass("grow-one")) {
			$animatedText.removeClass("grow-one");
		} else {
			$animatedText.removeClass("grow-two");
		}
		if (this.checked && !$animatedText.hasClass("grow-two")) {
			$animatedText.addClass("grow-two");
		} else {
		
			if ($animatedText.hasClass("grow-two")) {
				$animatedText.removeClass("grow-two");
			}
			$animatedText.addClass("grow-one");
		}
	});

})
.grow-in.grow-one {
    -webkit-animation-name: grow-one;
    animation-name: grow-one;
}

.grow-in.grow-two {
  -webkit-animation-name: grow-two;
    animation-name: grow-two;
}


.grow-in {
    -webkit-transform: scale(0.2);
    transform: scale(0.2);
    opacity: 0;
}

.slower {
    -webkit-animation-duration: 2s;
    animation-duration: 2s;
    -webkit-animation-fill-mode: both;
    animation-fill-mode: both;
}

.animated {
    -webkit-animation-duration: 1s;
    animation-duration: 1s;
    -webkit-animation-fill-mode: both;
    animation-fill-mode: both;
}

h1 {
    font-family: 'Lato', sans-serif;
    font-size: 32px;
    line-height: 62px;
    font-weight: 300;
    color: #000;
    text-align: center;
}

.checkboxes {
  text-align: center;
}


@keyframes grow-one {
    0% {
    transform: scale(0.2);
    opacity: 0;
    }
  
  50% {
    transform: scale(1.2);
  }
  
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes grow-two {
   100% {
    transform: scale(0.2);
    opacity: 0;
    }
  
  50% {
    transform: scale(1.2);
  }
  
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
<h1 class="animated grow-in slower" id="animatedText">CSS3 Animate It</h1>


<div class="checkboxes">
  <div class="checkbox-item">
    <span>item1</span>
    <input type="checkbox" class="checkbox">
  </div>
  <div class="checkbox-item">
    <span>item2</span>
    <input type="checkbox" class="checkbox">
  </div>
  <div class="checkbox-item">
    <span>item3</span>
    <input type="checkbox" class="checkbox">
  </div>
  <div class="checkbox-item">
    <span>item4</span>
    <input type="checkbox" class="checkbox">
  </div>
  <div class="checkbox-item">
    <span>item5</span>
    <input type="checkbox" class="checkbox">
  </div>
  <div class="checkbox-item">
    <span>item6</span>
    <input type="checkbox" class="checkbox">
  </div>
</div>

最佳答案

如果您将 $checkbox.change() 函数替换为此,它会按照您描述的方式工作:

$checkbox.change(function () {
    $animatedText.toggleClass("grow-two");
  });

https://codepen.io/anon/pen/KojjyL?editors=1010

关于javascript - jquery 检查事件和 css 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49846771/

相关文章:

javascript - 宏弹出式YouTube视频未链接

javascript - 我的 jQuery 函数互相破坏

PHP 和 JQuery : value not updating

javascript - 如何改变鼠标移动的渐变程度?

javascript - 获取新创建的 html 属性的值

php - 2个sql在一个php while循环中

javascript从 Mongoose 模式 Hook 中访问表单数据

javascript - 为ng-repeat添加的元素添加js

jquery - CSS 或 jQuery : Make last div fill the rest of the screen height

html - 在 HTML/CSS 中实现下拉列表