简体   繁体   English

如何使用JavaScript / CSS为DOM元素设置动画

[英]How to animate DOM elements with JavaScript/CSS

I'm working on a chess game with some strategic visual overlays. 我正在开发具有战略性视觉效果的国际象棋游戏。 One of them requires some light animation of pieces and squares, specifically, a slow and steady pulsing. 其中之一需要一些轻巧的碎片和正方形动画,特别是缓慢而稳定的脉冲。 My board is a with each being a single square. 我的木板是一个,每个都是一个正方形。 Pieces are shown by setting the background-image in CSS to an .svg of the piece it contains. 通过将CSS中的背景图像设置为其所包含的作品的.svg,可以显示作品。

Can this be done in CSS? 可以在CSS中完成吗? I'm using the latest browsers with no support for legacies, so I can use all the nifty CSS3 stuff. 我使用的是不支持旧功能的最新浏览器,因此我可以使用所有漂亮的CSS3。 Another option I was thinking was to set the background-image of the board to an animated .gif of the piece pulsing. 我当时想的另一种选择是将电路板的背景图像设置为脉动的动画.gif。 Would this work? 这行得通吗?

Are there any other ways to do this I haven't mentioned? 还有其他没有提及的方法吗? I would like to avoid packages/frameworks, but I am using jQuery. 我想避免使用包/框架,但是我使用的是jQuery。

CLARIFICATION: 澄清:

I want to make the chess piece kind of pulse (flash?) in place slowly for emphasis. 我想慢慢地使象棋棋子产生某种脉冲(闪光?),以便强调。 I want it to be a slow, subtle, and consistent pulse that persists until another event turns it off. 我希望它是一个缓慢,微妙且一致的脉冲,一直持续到另一个事件将其关闭为止。

It sounds like you're looking for CSS animations. 听起来您正在寻找CSS动画。

Take a look here: http://www.w3.org/TR/css3-animations 在这里看看: http : //www.w3.org/TR/css3-animations

In particular you'll need the following timing functions: 特别是,您需要以下计时功能:

  • animation-name , to specify the set of keyframes to use. animation-name ,指定要使用的关键帧集。
  • animation-duration , to specify the speed of the animation. animation-duration ,指定动画的速度。
  • animation-iteration-count , to repeat the animation. animation-iteration-count ,以重复动画。
  • animation-direction , to alternate the direction of the animation. animation-direction ,以改变animation-direction

And you'll need to create some keyframes, which let you specify what CSS properties are modified by the animation. 而且,您需要创建一些关键帧,以便您指定动画修改了哪些CSS属性。

Also, you'll need vendor prefixes on everything, so you need to write -webkit-animation-name rather than animation-name (for example), and repeat everything for -moz and other vendors. 此外,您还需要在所有内容上加上供应商前缀,因此您需要编写-webkit-animation-name而不是animation-name ,并为-moz和其他供应商重复所有内容。

Here's an example for webkit that creates a pulsating opacity effect. 这是一个创建脉动不透明效果的webkit示例。 You can experiment with the properties in the from and to sections to animate size, color, etc. 您可以在“ from和“ to部分中尝试使用属性来设置大小,颜色等的动画。

.chess-piece {
    -webkit-animation-duration: 1s;
    -webkit-animation-name: pulse;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-direction: alternate;
}
@-webkit-keyframes pulse {
    from {
        opacity: 0.5;
    }
    to {
        opacity: 1;
    }
}

JSFiddle example for webkit and moz Webkit和Moz的JSFiddle示例

That is quite possible and easy to do. 那是很可能的,而且容易做到。 As I understand it, you want to make some kind of glowing animation of the active chess figure? 据我了解,您想为活跃的国际象棋人物制作发光的动画吗? Here's how I would go implementing a solution: Create 2 pngs, one with the chess figure in its unflashy ordinary state and one with its fully illuminated state. 这是我实施解决方案的方式:创建2个png,其中一个象棋人物处于不浮华的普通状态,另一个则具有完全照明的状态。 Then use jQuery to change the opacity of the illuminated state from 0 to 100 and back to 0 again.You can send the jQuery "on animation finish" signals to achieve this. 然后使用jQuery将照明状态的不透明度从0更改为100,然后再次更改为0。您可以发送jQuery“动画完成时”信号来实现此目的。 You can than just use some kind of simplified observer pattern to cancel the effect once an event occurs. 您不仅可以使用某种简化的观察者模式在事件发生后取消效果。

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

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