Skip to content

Commit 2897a5b

Browse files
eltonmesquitadaneden
authored andcommitted
Update examples to pure js. closes #875 (#899)
I keept things simple and dropped support for aging browsers in the examples, although IE11 still gets support.
1 parent c03507b commit 2897a5b

1 file changed

Lines changed: 27 additions & 59 deletions

File tree

README.md

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -84,87 +84,55 @@ It's possible to change the duration of your animations, add a delay or change t
8484
}
8585
```
8686

87-
## Usage with jQuery
87+
## Usage with Javascript
8888

89-
You can do a whole bunch of other stuff with animate.css when you combine it with jQuery. A simple example:
89+
You can do a whole bunch of other stuff with animate.css when you combine it with Javascript. A simple example:
9090

9191
```javascript
92-
$('#yourElement').addClass('animated bounceOutLeft');
92+
const element = document.querySelector('.my-element')
93+
element.classList.add('animated', 'bounceOutLeft')
9394
```
9495

9596
You can also detect when an animation ends:
9697

97-
<!--
98-
Before you make changes to this file, you should know that $('#yourElement').one() is *NOT A TYPO*
99-
100-
http://api.jquery.com/one/
101-
-->
102-
10398
```javascript
104-
// See https://github.com/daneden/animate.css/issues/644
105-
var animationEnd = (function(el) {
106-
var animations = {
107-
animation: 'animationend',
108-
OAnimation: 'oAnimationEnd',
109-
MozAnimation: 'mozAnimationEnd',
110-
WebkitAnimation: 'webkitAnimationEnd',
111-
};
112-
113-
for (var t in animations) {
114-
if (el.style[t] !== undefined) {
115-
return animations[t];
116-
}
117-
}
118-
})(document.createElement('div'));
99+
const element = document.querySelector('.my-element')
100+
element.classList.add('animated', 'bounceOutLeft')
119101

120-
$('#yourElement').one(animationEnd, doSomething);
102+
element.addEventListener('animationend', function() { doSomething() })
121103
```
122104

123-
[View a video tutorial](https://www.youtube.com/watch?v=CBQGl6zokMs) on how to use Animate.css with jQuery here.
105+
You can use this simple function to add and remove the animations:
124106

125-
**Note:** `jQuery.one()` is used when you want to execute the event handler at most _once_. More information [here](http://api.jquery.com/one/).
107+
```javascript
108+
function animateCss(element, animationName, callback) {
109+
const node = document.querySelector(element)
110+
node.classList.add('animated', animationName)
126111

127-
It's possible to extend jQuery and add a function that does it all for you:
112+
function handleAnimationEnd() {
113+
node.classList.remove('animated', animationName)
114+
node.removeEventListener('animationend', handleAnimationEnd)
128115

129-
```javascript
130-
$.fn.extend({
131-
animateCss: function(animationName, callback) {
132-
var animationEnd = (function(el) {
133-
var animations = {
134-
animation: 'animationend',
135-
OAnimation: 'oAnimationEnd',
136-
MozAnimation: 'mozAnimationEnd',
137-
WebkitAnimation: 'webkitAnimationEnd',
138-
};
139-
140-
for (var t in animations) {
141-
if (el.style[t] !== undefined) {
142-
return animations[t];
143-
}
144-
}
145-
})(document.createElement('div'));
146-
147-
this.addClass('animated ' + animationName).one(animationEnd, function() {
148-
$(this).removeClass('animated ' + animationName);
149-
150-
if (typeof callback === 'function') callback();
151-
});
152-
153-
return this;
154-
},
155-
});
116+
if (typeof callback === 'function') callback()
117+
}
118+
119+
node.addEventListener('animationend', handleAnimationEnd)
120+
}
156121
```
157122

158123
And use it like this:
159124

160125
```javascript
161-
$('#yourElement').animateCss('bounce');
162-
or;
163-
$('#yourElement').animateCss('bounce', function() {
126+
animateCSS('.my-element', 'bounce')
127+
128+
// or
129+
animateCSS('.my-element', 'bounce', function() {
164130
// Do something after animation
165-
});
131+
})
166132
```
167133

134+
Notice that the examples are using ES6's `const` declaration, dropping support for IE10 and some aging browsers. If you prefer, switch the `const` to `var` declarations and IE10 and some old browsers will get support (they still have to provide [classList](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList) support, so do your [research](https://caniuse.com/#feat=classlist)).
135+
168136
## Setting _Delay_ and _Speed_
169137

170138
### Delay Class

0 commit comments

Comments
 (0)