Skip to content

Commit 0343e22

Browse files
committed
Add animation class
1 parent 23b0767 commit 0343e22

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"liveServer.settings.port": 5501
3+
}

neptune-animations.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.neptune-spin {
2+
animation: spin 1s 0s infinite;
3+
ani
4+
}
5+
6+
@keyframes spin {
7+
100% { transform: rotate(360deg);}
8+
}
9+
10+
11+
12+
.test {
13+
margin: 15rem;
14+
width: 4rem;
15+
height: 4rem;
16+
background-color: aqua;
17+
}

neptune-animations.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export default class NeptuneAnimate {
2+
constructor(element, animationClass) {
3+
this.element = element;
4+
this.animationClass = animationClass;
5+
this.animationEndEvent = this.getAnimationEndEvent();
6+
7+
this.element.addEventListener(this.animationEndEvent, this.handleAnimationEnd.bind(this));
8+
}
9+
10+
getAnimationEndEvent() {
11+
const el = document.createElement("div");
12+
const animations = {
13+
animation: "animationend",
14+
OAnimation: "oAnimationEnd",
15+
MozAnimation: "animationend",
16+
WebkitAnimation: "webkitAnimationEnd"
17+
};
18+
19+
for (const key in animations) {
20+
if (el.style[key] !== undefined) {
21+
return animations[key];
22+
}
23+
}
24+
25+
return "animationend";
26+
}
27+
28+
handleAnimationEnd() {
29+
this.element.classList.remove(this.animationClass);
30+
this.element.removeEventListener(this.animationEndEvent, this.handleAnimationEnd);
31+
}
32+
33+
startAnimation() {
34+
this.element.classList.add(this.animationClass);
35+
}
36+
37+
stopAnimation() {
38+
this.element.classList.remove(this.animationClass);
39+
}
40+
}

test/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="de">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
7+
<link rel="stylesheet" href="../neptune-animations.css">
8+
9+
<title>Document</title>
10+
</head>
11+
<body>
12+
<div class="test" id="test"></div>
13+
14+
<script type="module">
15+
import NeptuneAnimate from "../neptune-animations.js"
16+
17+
const myElement = document.getElementById("test");
18+
const neptuneAnimate = new NeptuneAnimate(myElement, "neptune-spin");
19+
20+
neptuneAnimate.startAnimation();
21+
22+
setTimeout(function () {
23+
neptuneAnimate.stopAnimation();
24+
}, 5000);
25+
</script>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)