4
4
# waitForElementTransition()
5
5
6
6
Let'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.
10
10
11
11
## Benefits
12
12
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
18
18
19
19
## Installation
20
20
21
21
You can either use the dist file directly in your project:
22
22
23
23
``` 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 >
25
28
```
26
29
27
30
Or install via npm
@@ -36,58 +39,55 @@ and import
36
39
import waitForElementTransition from ' wait-for-element-transition' ;
37
40
```
38
41
39
-
40
42
## Usage
41
43
42
44
Here's an example where we want to wait for an element's background color to change from black to red.
43
45
44
46
``` html
45
47
<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
+ }
57
58
</style >
58
59
59
60
<div >Transition this element</div >
60
61
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 >
62
66
<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
+ });
69
73
</script >
70
-
71
74
```
72
75
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
74
77
promise will resolve immediately. So you can always guarantee that your code will run, just as it would synchronously.
75
78
76
-
77
79
## Alternatives
78
80
79
81
### The ` transitionend ` event
80
82
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
82
84
has ended, but this approach is limited:
83
85
84
86
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
85
- the css property is removed) and
87
+ the css property is removed) and
86
88
1 . 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.
88
90
89
91
### Web Animations
90
92
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