简体   繁体   English

CSS3 onclick动画

[英]CSS3 onclick animation

Trying to learn the basics of CSS3 animation and onclick events but I'm stuck. 试图学习CSS3动画和onclick事件的基础知识,但我陷入了困境。

I want to use button id="3" to start the animation, the red square as you as can see here: https://jsfiddle.net/4swmegpn/ And button id="4" to stop/reset the animation of the red square. 我想使用按钮id =“ 3”来启动动画,如您在此处看到的那样,用红色正方形表示: https : //jsfiddle.net/4swmegpn/和按钮id =“ 4”来停止/重置动画红方格。

I just don't know where to start. 我只是不知道从哪里开始。 I've started with adding onclick to: 我开始添加onclick到:

<button id="button1" onClick=""> > </button>

But after that I can't come up with what to do. 但是在那之后,我无法提出该做什么。 Something to call and activate "keyframes example" from the css-file, but not sure how to. 可以从css文件中调用和激活“关键帧示例”的东西,但是不确定如何操作。

You can add and remove class to the element to start / end animation. 您可以在元素中添加和删除类以开始/结束动画。 And in CSS add animation to that class (see fiddle ). 并在CSS中向该类添加动画(请参阅fiddle )。 Also it is a good practice not to include any javascript code in HTML (like onclick you had), but rather add event listeners in scripts: 另外,最好不要在HTML中包含任何javascript代码(例如您拥有的onclick),而是在脚本中添加事件侦听器:

document.getElementById('startAnim').addEventListener('click', function() {
    document.getElementById('red').classList.add('animate');
});

document.getElementById('stopAnim').addEventListener('click', function() {
    document.getElementById('red').classList.remove('animate');
});

You can also use checkbox, hover state or other selectors to toggle animation from within CSS. 您还可以使用复选框,悬停状态或其他选择器从CSS内切换动画。 There are lots of techniques. 有很多技术。 It all depends on what you need to achieve. 这完全取决于您需要实现的目标。

Here are some pointers before we start talking about the animation 这是我们开始谈论动画之前的一些提示

  • You don't need to type doctype and also head , and body tags because the jsfiddle platform do it for you. 您不需要键入doctype以及headbody标记,因为jsfiddle平台可以为您完成此操作。

  • It is good practice to escape < and > with &lt; 优良作法是使用&lt;转义<> &lt; and &gt; &gt; because they can break your HTML code. 因为它们会破坏您的HTML代码。

  • For CSS3 stuff like the animations it is good practice to use Vendor Prefixes , this are browser-specific prefixes that will enable the behavior you are looking for in that browser. 对于CSS3之类的动画,最好使用Vendor Prefixes ,这是特定于浏览器的前缀,可以使您在该浏览器中寻找所需的行为。 Currently there are: 目前有:

    1. Chrome & Safari: -webkit- Chrome和Safari:-webkit-
    2. Firefox: -moz- Firefox:-moz-
    3. Opera: -o- 歌剧:-o-
    4. Internet Explorer: -ms- Internet Explorer:-ms-

It is a good practice to leave the non-prefixed CSS3 code for last because it will override all browser-specific stuff above it. 最好将非前缀的CSS3代码留在最后,因为它会覆盖上面的所有特定于浏览器的内容。 So here is an example of what your key-frames should look like: 因此,这是一个关键帧外观示例:

@-webkit-keyframes tutsFade { /* your style */ }
@-moz-keyframes tutsFade { /* your style */ }
@-ms-keyframes tutsFade { /* your style */ }
@-o-keyframes tutsFade { /* your style */ }
@keyframes tutsFade { /* your style */ }

Now on the animation part - I've created a simplified version of your example here - https://jsfiddle.net/a1d1Lk6f/ What is new is the .exampleAnimation class that contains all you need for an element to start animating with the example animation defined at the bottom of the CSS file. 现在的动画部分-我已经创建了实例的简化版本,在这里- https://jsfiddle.net/a1d1Lk6f/什么是新是.exampleAnimation包含所有你需要的元素开始与动画类example动画定义在CSS文件的底部。 You can add this class directly to an HTML element or control it form JavaScript to start/stop the animation. 您可以将此类直接添加到HTML元素中,也可以通过JavaScript对其进行控制以启动/停止动画。

Here are some basic tutorials for beginners: Introduction to CSS Animation , Control CSS3 Animations With jQuery . 以下是一些适合初学者的基本教程: CSS动画介绍使用jQuery控制CSS3动画

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

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