JavaScripting

The definitive source of the best
JavaScript libraries, frameworks, and plugins.


  • ×

    Animate your 'action-to-effect' paths
    Filed under 

    • 🔾37%Overall
    • 1,650
    • 70.3 days
    • 🕩144
    • 👥5

    cta.js npm version

    A call to animate, your action-to-effect path


    cta.js or "Call to Animation" is a light-weight performant library to animate any element ("action") onto any other element ("effect") on the page.

    It is written with an aim to promote visual continuity in web apps. To see what you can do with this, checkout the demo:

    DEMO

    Installation

    cta.js is just 1.2KB minified & gzipped.

    • Bower: bower install cta
    • NPM: npm install cta
    • Download zip.

    Note: cta.js supports AMD and commonJS module pattern out of the box.

    Usage

    In very basic form, you can animate an element with selector X onto an element with selector Y:

    var e1 = document.querySelector(X),
        e2 = document.querySelector(Y);
    cta(e1, e2);
    

    Triggering a reverse animation;

    var e1 = document.querySelector('#js-source-element'),
        e2 = document.querySelector('#js-target-element');
    var reverseAnimate = cta(e1, e2);
    
    // Reverse previous animation. `options` and `callback` can be passed to this function too.
    reverseAnimate();
    

    Specify animation duration:

    var e1 = document.querySelector('#js-source-element'),
        e2 = document.querySelector('#js-target-element');
    cta(e1, e2, {
        duration: 0.3 // seconds
    });
    

    Specify a callback to execute after animation:

    var button = document.querySelector('#js-button'),
        hiddenModal = document.querySelector('#js-modal');
    cta(button, hiddenModal, function () {
        showModal();
    });
    

    More documentation coming up.

    Public API

    cta(sourceElement, targetElement [, options] [, callback] )

    Animate an element sourceElement onto targetElement.

    • sourceElement - DOM Element which is the starting point of animation.
    • targetElement - DOM Element which is the end point of animation.
    • options - A map of additional options to control the animation behaviour.
      • duration - Duration (in seconds) of animation. Default is 0.3 seconds.
      • targetShowDuration - Duration (in seconds) of targetElement to become visible, if hidden initially. The library will automatically try to figure this out from the element's computed styles. Default is 0 seconds.
      • extraTransitionDuration - Extra duration (in seconds) of targetElement to provide visual continuity between the animation and the rendering of the targetElement. Default is 1 second.
      • relativeToWindow - Set to true if your target element is fixed positioned in the window. Default is relative to document (works good with normal elements).
    • callback - Optional callback to execute after animation completes.

    Browser Support

    cta.js works best on latest versions of Google Chrome, Firefox and Safari.

    For all non-supported browsers, the library does nothing and fallbacks to normal behavior without any explicit handling in your code.

    Contributing

    Interested in contributing features and fixes?

    Read more on contributing.

    Changelog

    See the Changelog

    License

    Copyright (c) 2015-2016 Kushagra Gour, http://kushagragour.in Licensed under the MIT license.

    Credits

    Paul Lewis - for his awesome performance tip on scaling elements.

    Show All