-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (54 loc) · 1.32 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
'use strict'
/**
* Lets a element blink based on the speed provided
*/
module.exports = function blink (args = { speed: 0.01 }) {
if (!args.element) { throw new Error('No element provided') }
let isStopped = false
args.element.style.opacity = 1
const loop = () => {
if (!isStopped) {
fadeIn(args.element, args.speed, () => {
fadeOut(args.element, args.speed, () => loop())
})
}
}
loop()
return {
start: () => {
isStopped = false
loop()
},
stop: () => {
isStopped = true
}
}
}
/**
* Get requestAnimationFrame for env, otherwise use setTimeout
*/
const raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || ((cb) => {
window.setTimeout(cb, 1000 / 60)
})
/**
* Peform fade out animation on element based on speed
*/
function fadeOut (elem, speed, cb) {
elem.style.opacity = 1
const fade = () => {
elem.style.opacity -= speed
elem.style.opacity < 0 ? cb() : raf(fade)
}
fade()
}
/**
* Peform fade in animation on element based on speed
*/
function fadeIn (elem, speed, cb) {
elem.style.opacity = 0
const fade = () => {
elem.style.opacity = parseFloat(elem.style.opacity) + speed
elem.style.opacity > 1 ? cb() : raf(fade)
}
fade()
}