blackChef / jkf

A javascript animation library that use css-keyframes-animation syntax
3 stars 0 forks source link

Jkf

A javascript animation library that use css-keyframes-animation syntax. examples

Keyframes

这是在 Jkf 里使用的 keyframes:

{
    0: { left: 0, borderRadius: 0 },
    0.5: { left: '30px' },
    1: { left: '100px', borderRadius: '50%' }
}

它是一个长得很像 css-keyframes 的 javascript object。值得注意的是:

Combinations

transform: translateX(0) rotateZ(10deg) scale(1.2)

transform 可以被当作由 translateX, rotateZ, scale 三个子属性组成。我们把像这样能拆分成多个子属性的属性叫做 combination。

combination 的子属性未必是要“真实”存在的。

background-color: rgb(0, 0, 0)

这里可以认为 background-color 是由 r, g, b 三个子属性组成。

Jkf 提供了 Jkf.registerCombination 方法,允许你自定义 combination。

因为 transform 被广泛应用,Jkf 预先把它注册成了 combination。你可以像这样在 keyframes 里使用 transform 了:

{
  0: { translateX: 0, rotateZ: 0, borderRadius: 0, opacity: 1 },
  0.5: { borderRadius: '50%', opacity: 0.5 },
  1: { translateX: '100%', rotateZ: '360deg', borderRadius: 0, opacity: 1 }
};

Jkf 内部是如何注册 transform 的 如何把 background-color 注册成 combination

Usage

Jkf.update(elem, keyframes, progress)

给定 progress,把元素 style 成 keyframes 里相对应的状态。example

Jkf.animate(elem, keyframes, duration [, options]) => controller

进行一段基于 keyframes 的动画。通过函数返回的 controller,可以对已经开始的动画进行控制。

Jkf.queuedAnimate(elem, ...animationConfigs, callback)

Jkf.queuedAnimate(elem, [
  [kf1, duration1, options1, delay1],
  [kf2, duration2, options2, delay2],
  [kf3, duration3, options3, delay3],
  ...
], callback);

Delay is optional. Callback is called when last animation ended;