Coder Social home page Coder Social logo

markcellus / wait-for-element-transition Goto Github PK

View Code? Open in Web Editor NEW
19.0 0.0 1.0 1.42 MB

Easily wait for an element to CSS transition using native vanilla JavaScript

License: MIT License

JavaScript 22.12% TypeScript 77.88%
css transition transitionend transition-animation wait polymer-element css-animation transition-property react-animation element-transition

wait-for-element-transition's Introduction

Build Status npm version

waitForElementTransition()

Let's say you have the problem (that many of us have) where you need to wait for the completion of your element's CSS transition before your code continues. You can use this library and call a waitForElementTransition method to wait until the element finishes its css transition before doing other things in your javascript code.

Benefits

  • Easily wait for an element's css transition to end using JavaScript
  • Allows you to keep your transition/animation css properties separate from your JS
  • Native javascript with no dependencies
  • Safer and more reliable than transitionstart and transitionend events
  • Plays nicely with the latest specifications

Installation

You can either use the dist file directly in your project:

<script
    type="text/javascript"
    src="/dist/wait-for-element-transition.min.js"
></script>

Or install via npm

npm i wait-for-element-transition

and import

import waitForElementTransition from 'wait-for-element-transition';

Usage

Here's an example where we want to wait for an element's background color to change from black to red.

<style>
    div {
        background-color: black;
        transition-property: background-color;
        transition-duration: 100ms;
        transition-timing-function: ease-out;
    }

    .red {
        background-color: red;
    }
</style>

<div>Transition this element</div>

<script
    type="text/javascript"
    src="/dist/wait-for-element-transition.min.js"
></script>
<script>
    const element = document.querySelector('div');
    element.classList.add('red'); // start transition
    waitForElementTransition(element).then(() => {
        // 100 milliseconds later...
        console.log('element background color changed to red!');
    });
</script>

If the element has already transitioned before the waitForElementTransition() is called, the waitForElementTransition()s promise will resolve immediately. So you can always guarantee that your code will run, just as it would synchronously.

Alternatives

The transitionend event

Using the transitionend or animationend events on an Element will allow you to wait until an Element's transition has ended, but this approach is limited:

  1. The events don't fire in the case where a transition is removed before completion (i.e. display is set to "none" or if the css property is removed) and
  2. The events don't fire when there are no css transition properties specified, which doesn't allow us to run the same animation-completion logic on elements which may or may not be animated.

Web Animations API

If done in a supported browser, the same thing in this package can be achieved using the Web Animations API. But consumers of this package may want a less-complex approach.

Development

Watch source files while developing:

npm start

Run test

npm test

wait-for-element-transition's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar markcellus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dword-design

wait-for-element-transition's Issues

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.