简体   繁体   English

使用Javascript激活CSS3动画

[英]Using Javascript to activate CSS3 animations

I was playing around with some CSS animations and I'm trying to trigger the animations using a Javascript onclick. 我正在玩一些CSS动画,并尝试使用Javascript onclick触发动画。 Noting seems to be working for me. 注意似乎正在为我工​​作。

http://codepen.io/Quinn1011/pen/Eltpk http://codepen.io/Quinn1011/pen/Eltpk

Could someone please take a look and help me out. 有人可以看看并帮助我。 I can't even get "next" div to hide on pageload. 我什至无法在页面加载中隐藏“下一个” div。

add the following line to the css of #next : 将以下行添加到#next的CSS中:

display:none 

wow , that was easy, now the next show´s up on click and not on load. ,那很容易,现在下一个节目是在点击而不是在加载时。

.show() is nothing else than removing the css property display:none. .show()就是删除css属性display:none。 just like .hide() is adding display:none to the element 就像.hide()向元素添加display:none一样

if you understood that, it will be easy for you to understand that your animations are running on load because the css has already been set 如果您了解这一点,那么您将很容易理解动画是在加载时运行的,因为已经设置了css

so on click add the suiting classes to your elements 所以点击添加合适的类到您的元素

http://api.jquery.com/addClass/ http://api.jquery.com/addClass/

ah sorry, that would be the jquery-solution, if you´re not using jQuery and you didn´t mention it, heres the pure way : 抱歉,这是jquery-solution,如果您不使用jQuery却没有提到它,那么这就是纯方法:

function changeClass (elementID, newClass) {
var element = document.getElementById(elementID);

element.setAttribute("class", newClass); 
}

you can call this function like: 您可以像下面这样调用此函数:

$('#button').click(function(){
  changeClass('next','anmimate');
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM