forked from kontur-web-courses/animaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (94 loc) · 3.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
addListeners();
function animaster(){
let obj = {
fadeOut(element, duration) {
element.style.transitionDuration = `${duration}ms`;
element.classList.add('hide');
element.classList.remove('show');
},
fadeIn(element, duration) {
element.style.transitionDuration = `${duration}ms`;
element.classList.remove('hide');
element.classList.add('show');
},
move(element, duration, translation) {
element.style.transitionDuration = `${duration}ms`;
element.style.transform = getTransform(translation, null);
},
scale(element, duration, ratio) {
element.style.transitionDuration = `${duration}ms`;
element.style.transform = getTransform(null, ratio);
},
showAndHide(element, duration) {
this.fadeIn(element, duration / 3)
setTimeout(this.fadeOut, 2 * duration/3, element, duration / 3)
},
heartBeating(element){
let a = setInterval(() => this.scale(element, 500, 1.4), 500);
let b = setInterval(() => this.scale(element, 500, 1), 1000);
let heart = {
stop (){
clearInterval(a)
clearInterval(b)
}
}
return heart
},
moveAndHide(element, duration) {
this.move(element, duration / 5 * 2, {x: 100, y: 20})
setTimeout(this.fadeOut, duration / 5 * 2, element, duration / 5 * 3)
}
}
return obj
}
function addListeners() {
document.getElementById('fadeInPlay')
.addEventListener('click', function () {
const block = document.getElementById('fadeInBlock');
animaster().fadeIn(block, 5000);
});
document.getElementById('movePlay')
.addEventListener('click', function () {
const block = document.getElementById('moveBlock');
animaster().move(block, 1000, {x: 100, y: 10});
});
document.getElementById('scalePlay')
.addEventListener('click', function () {
const block = document.getElementById('scaleBlock');
animaster().scale(block, 1000, 1.25);
});
document.getElementById('fadeOutPlay')
.addEventListener('click', function () {
const block = document.getElementById('fadeOutBlock');
animaster().fadeOut(block, 5000);
});
document.getElementById('showAndHidePlay')
.addEventListener('click', function () {
const block = document.getElementById('showAndHideBlock');
animaster().showAndHide(block, 5000);
});
document.getElementById('heartBeatingPlay')
.addEventListener('click', function () {
const block = document.getElementById('heartBeatingBlock');
let heart = animaster().heartBeating(block)
document.getElementById('heartBeatingStop')
.addEventListener('click', function () {
heart.stop();
});
});
document.getElementById('moveAndHidePlay')
.addEventListener('click', function () {
const block = document.getElementById('moveAndHideBlock');
animaster().moveAndHide(block, 5000);
});
}
function getTransform(translation, ratio) {
const result = [];
if (translation) {
result.push(`translate(${translation.x}px,${translation.y}px)`);
}
if (ratio) {
result.push(`scale(${ratio})`);
}
return result.join(' ');
}