44# waitForElementTransition()
55
66Let's say you have the problem (that many of us have) where you need to wait for the completion of your element's
7- [ CSS transition] ( https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions )
8- before your code continues. You can use this library and call a ` waitForElementTransition ` method to wait until
9- the element finishes its css transition before doing other things in your javascript code.
7+ [ CSS transition] ( https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions )
8+ before your code continues. You can use this library and call a ` waitForElementTransition ` method to wait until
9+ the element finishes its css transition before doing other things in your javascript code.
1010
1111## Benefits
1212
13- * Easily wait for an element's css transition to end using JavaScript
14- * Allows you to keep your transition/animation css properties separate from your JS
15- * Native javascript with no dependencies
16- * Safer and more reliable than ` transitionstart ` and ` transitionend ` events
17- * Plays nicely with the latest specifications
13+ - Easily wait for an element's css transition to end using JavaScript
14+ - Allows you to keep your transition/animation css properties separate from your JS
15+ - Native javascript with no dependencies
16+ - Safer and more reliable than ` transitionstart ` and ` transitionend ` events
17+ - Plays nicely with the latest specifications
1818
1919## Installation
2020
2121You can either use the dist file directly in your project:
2222
2323``` html
24- <script type =" text/javascript" src =" /dist/wait-for-element-transition.min.js" ></script >
24+ <script
25+ type =" text/javascript"
26+ src =" /dist/wait-for-element-transition.min.js"
27+ ></script >
2528```
2629
2730Or install via npm
@@ -36,58 +39,55 @@ and import
3639import waitForElementTransition from ' wait-for-element-transition' ;
3740```
3841
39-
4042## Usage
4143
4244Here's an example where we want to wait for an element's background color to change from black to red.
4345
4446``` html
4547<style >
46-
47- div {
48- background-color : black ;
49- transition-property : background-color;
50- transition-duration : 100ms ;
51- transition-timing-function : ease-out ;
52- }
53-
54- .red {
55- background-color : red ;
56- }
48+ div {
49+ background-color : black ;
50+ transition-property : background-color;
51+ transition-duration : 100ms ;
52+ transition-timing-function : ease-out ;
53+ }
54+
55+ .red {
56+ background-color : red ;
57+ }
5758 </style >
5859
5960<div >Transition this element</div >
6061
61- <script type =" text/javascript" src =" /dist/wait-for-element-transition.min.js" ></script >
62+ <script
63+ type =" text/javascript"
64+ src =" /dist/wait-for-element-transition.min.js"
65+ ></script >
6266<script >
63- const element = document .querySelector (' div' );
64- element .classList .add (' red' ); // start transition
65- waitForElementTransition (element).then (() => {
66- // 100 milliseconds later...
67- console .log (' element background color changed to red!' );
68- });
67+ const element = document .querySelector (' div' );
68+ element .classList .add (' red' ); // start transition
69+ waitForElementTransition (element).then (() => {
70+ // 100 milliseconds later...
71+ console .log (' element background color changed to red!' );
72+ });
6973 </script >
70-
7174```
7275
73- If the element has already transitioned before the ` waitForElementTransition() ` is called, the ` waitForElementTransition() ` s
76+ If the element has already transitioned before the ` waitForElementTransition() ` is called, the ` waitForElementTransition() ` s
7477promise will resolve immediately. So you can always guarantee that your code will run, just as it would synchronously.
7578
76-
7779## Alternatives
7880
7981### The ` transitionend ` event
8082
81- Using the ` transitionend ` or ` animationend ` events on an Element will allow you to wait until an Element's transition
83+ Using the ` transitionend ` or ` animationend ` events on an Element will allow you to wait until an Element's transition
8284has ended, but this approach is limited:
8385
84861 . The events don't fire in the case where a transition is removed before completion (i.e. display is set to "none" or if
85- the css property is removed) and
87+ the css property is removed) and
86881 . The events don't fire when there are no css transition properties specified, which doesn't allow us to run the
87- same animation-completion logic on elements which may or may not be animated.
89+ same animation-completion logic on elements which may or may not be animated.
8890
8991### Web Animations
9092
91- - Not supported in all browsers like Internet Explorer or Safari
92-
93-
93+ - Not supported in all browsers like Internet Explorer or Safari
0 commit comments