-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
53 lines (40 loc) · 1.67 KB
/
script.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
import Animation from './animation.js';
import AnimationFrame from './animationFrame.js';
/* ----------------------------------------------------------------*/
/* Initialize click and hold elements */
const holdDuration = 500; // 500ms
const callbacks = {onHoldStart, onHoldComplete, onHoldCancel};
/* CSS transition */
const btn1El = document.querySelector('[data-btn="css-transition"]');
Animation(btn1El, holdDuration, callbacks);
/* requestAnimationFrame + CSS transform */
const btn2El = document.querySelector('[data-btn="animationFrame-css-transform"]');
AnimationFrame(btn2El, holdDuration, callbacks);
/* requestAnimationFrame + CSS transition */
const btn3El = document.querySelector('[data-btn="animationFrame-css-transition"]');
AnimationFrame(btn3El, holdDuration, callbacks);
/* ----------------------------------------------------------------*/
/* update DOM and display event */
const maxEventsToShow = 5;
const events = document.querySelector('.events-container');
function initInsertEvent(id) {
return function(e, name) {
const btnType = e.target.closest('.click-and-hold').getAttribute('data-btn');
const eventName = `<p data-btn=${btnType} class="event-text">${id}. Hold ${name}</p>`
if (events.children.length === maxEventsToShow) {
events.removeChild(events.children[0]);
}
events.insertAdjacentHTML('beforeend', eventName);
id++;
}
}
const insertEvent = initInsertEvent(0);
function onHoldStart(e) {
insertEvent(e, 'start');
}
function onHoldComplete(e) {
insertEvent(e, 'complete');
}
function onHoldCancel(e) {
insertEvent(e, 'cancel');
}