Code Monkey home page Code Monkey logo

css-animations.js's Introduction

css-animations.js

Create, Modify, and Remove CSS3 Keyframe Animations with javascript!

This library uses the CSS DOM API to access CSS3 keyframe animations, enabling you to do anything you want with them from javascript.

You can add, modify, and remove individual keyframes from existing animations, in addition to creating and deleting animations themselves.

Demos

Usage

Download css-animations.js to your project and load it. It works as an AMD module as well as a global object.

If not using it as an AMD module, it exports a global objects named CSSAnimations that allows you to access the API.

NOTE: This library searches all your stylesheets immediately when it is loaded. This will cause problems if you are somehow dynamically loading stylesheets after js is loaded (problems being missing animations). If this is common, I will change this library.

Browser Support

This is a new library and hasn't been extensively tested. It has only been tested in Firefox 17+ and Chrome 23+. It should work in browsers that implement unprefixed CSS3 animations and webkit (special prefixing has only been applied to webkit as it is not unprefixed yet).

API

Animations

  • CSSAnimations.get(name): return a KeyframeAnimation object representing the animation named name

  • CSSAnimations.create(name, frames): create a new animation named name and return the newly constructed KeyframeAnimation object. If frames is suppled, add them to the new animation with setKeyframes (see below). name is optional; if not specified a random name is generated. frames is also optional.

    // Create with name and keyframes
    var anim = CSSAnimations.create('foo', {
        '0%': { 'background-color': 'red' },
        '100%': { 'background-color': 'blue' }
    });
    
    // Create with keyframes, names is randomly generated
    var anim = CSSAnimations.create({
        '0%': { 'background-color': 'red' },
        '100%': { 'background-color': 'blue' }
    });
    
    // Create with just name and no keyframes
    var anim = CSSAnimations.create('foo');
    
    // Create with random name and no keyframes
    var anim = CSSAnimations.create();
  • CSSAnimations.remove(name): remove the animation named name. name can also be an instance of KeyframeAnimation. Right now, you can only remove animations created with CSSAnimations.create.

KeyframeAnimation

The KeyframeAnimation object represents a CSS3 animation.

  • KeyframeAnimation.getKeyframe(text): return a KeyframeRule object representing the animation at the specified keyframe. text is a string that represents the keyframe, such as "10%".

    var rule = anim.getKeyframe('10%');
  • KeyframeAnimation.setKeyframe(text, css): set the CSS for a specified keyframe. text is a string the represents the keyframes, like "10%", and css is a javascript object with key/values representing the CSS to set. It returns the same KeyframeAnimation object so you can chain it.

    anim.setKeyframe('10%': { 'background-color': '#333333' });
  • KeyframeAnimation.setKeyframes(frames): Same as setKeyframe, but sets multiple keyframes at once. frames is an object with the percentage values, like 10%, as keys and css as values.

    anim.setKeyframes({
        '10%': { 'background-color': '#333333' },
        '20%': { 'background-color': '#666666' },
    });
  • KeyframeAnimation.clear(): Remove all keyframes from this animation.

  • KeyframeAnimation.getKeyframeTexts(): Get all the texts representing the keyframe positions, like "10%" and "100%".

KeyframeRule

The KeyframeRule object represents a specific animation keyframe.

  • KeyframeRule.keyText: the text representing the keyframe position, like "10%"

  • KeyframeRule.css: a javascript object representing the CSS for this keyframe

NOTE: In several places we represent CSS as javascript objects, but it does not transform property names to camelCase formatting. The keys in the object are the raw CSS properties and you'll most likely have to quote them because they contain dashed. For example, css = { 'background-color': 'red' } and css['background-color'].

Examples

// Changing an animation

var anim = CSSAnimations.get('pulse');
anim.setKeyframe('100%', { 'background-color': 'red' });

// Dynamically creating and applying an animation

var anim = CSSAnimations.create({
    '0%': { transform: 'translateY(0)' },
    '70%': { transform: 'translateY(50px)' },
    '100%': { transform: 'translateY(150px)' }
});

$(el).css({ 'animation-name': anim.name,
            'animation-duration': '1s' });
            
$(el).on('animationend', function() {
    CSSAnimations.remove(anim.name);
});

githalytics.com alpha

css-animations.js's People

Contributors

jlongster avatar potch avatar

Stargazers

AlexAzTucson16 avatar  avatar  avatar Adnan Ahmed avatar vienues avatar Luiz Felipe  avatar  avatar KingxBeta avatar Sofya Tuymedova avatar Mashy avatar KGBDESIGNGLOBAL avatar Paulo Santos avatar Eryc avatar  avatar YASH GOSARA avatar YoungChief avatar  avatar  avatar Léopold Szabatura avatar Lê Trần Duy Tân avatar Antonio Gómez-Maldonado avatar  avatar Nitin Khanna avatar LCF avatar  avatar Emmanuel Château-Dutier avatar Kyrie Leee avatar  avatar Acampbell avatar David Danziger avatar Jake avatar L avatar Cat  avatar kallsave avatar JiZhi avatar Naonak avatar  avatar crixusshen avatar Matt Ryan avatar Maxime Beauchamp avatar  avatar  avatar  avatar Jeremy Sarda avatar mumu avatar Panghu avatar Steve avatar LEI avatar GAURAV avatar Felipe Appio avatar Ann-Kathrin Hietkamp avatar  avatar MatthewSun avatar  avatar Chris Wetterman avatar David Mignot avatar David Aerne avatar Ingvi Jonasson avatar George Raptis avatar Hosein avatar DaShun avatar Falmata (Mata) Mohamed avatar Alexander Berezhnoy avatar klouskingsley avatar Anthony Cossins avatar FS.IO avatar Sasha Tatar avatar Emrah Bilbay avatar Cheton Wu avatar Son Jungwook avatar Jakub Albert Ferenc avatar Lan Qingyong avatar akbarwibawa avatar WinkyZhu avatar Daniel Bayley avatar Hemerson Vianna avatar zhoushirong avatar Damian Senn avatar Jun Liu avatar Amol Meshram avatar Serhii Shyran avatar nhducit avatar Theoton avatar  avatar Nguyen Trung Tin avatar Igor Morais avatar Alex Zhang avatar Dennis Calazans avatar  avatar William Oliveira avatar Leonardo Nascimento avatar Jaon avatar  avatar Hasaki avatar  avatar jenner avatar cocoo.li avatar Kemeice Parson avatar  avatar Adrián Salgado avatar

Watchers

Facundo Cabrera avatar José Sánchez Moreno avatar Xavier Barbosa avatar  avatar Michael Paige avatar jack avatar 小咩 avatar Yiannis M avatar James Cloos avatar Kelsey Dawes avatar Krister Kari avatar Joe  avatar akbarwibawa avatar Julian Garnier avatar Joachim Kisun Song avatar Michael Anthony avatar  avatar  avatar Tony avatar Mashy avatar  avatar Chatchai Daecha avatar  avatar 阮野 avatar  avatar

css-animations.js's Issues

CSSAnimations.remove() broken

This doesn't seem to be working (anymore?). After the animation plays through the element still has the animation name, duration, etc. set.

I've tried it out in Chrome, Safari and FF.

insertRule doesn't exist in W3C CSSKeyframesRule interface

This doesn't work in Opera:
http://jsbin.com/esocaw/22/edit

There is a simple fix for that just change direct insertRule call in KeyframeAnimation.prototype.setKeyframe to:

("appendRule" in this.original) ? this.original.appendRule(cssRule) : this.original.insertRule(cssRule);

See also W3C spec:
http://dev.w3.org/csswg/css3-animations/#csskeyframesrule

Why you still need insertRule? Because WebKitCSSKeyframesRule has different interface :-/.
http://developer.apple.com/library/safari/#documentation/AudioVideo/Reference/WebKitCSSKeyframesRuleClassReference/WebKitCSSKeyframesRule/WebKitCSSKeyframesRule.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.