JSFiddle - React, Tailwind, and code Playground
by Diana Lemen
HTML
<div class="wrapper">
<div class="rect static"></div>
<div class="rect move"></div>
<div class="value" id="value"></div>
</div>
<button id="triggerUp">runUp</button>
<button id="triggerDown">runDown</button>
CSS
.rect {
position: absolute;
top: 0px;
width: 130px;
height: 100px;
}
.static {
background: black;
top: 50px;
border-bottom: 1px solid white;
}
.move {
top: 150px;
color: #ddd;
font-size: 3em;
line-height: 100px;
text-align: center;
background: #444;
transform-origin: 0 0;
}
.action {
animation: move 1s forwards;
}
.wrapper {
position: absolute;
left: 40%;
top: 0;
}
.value {
color: white;
position: absolute;
z-index: 999;
top: 125px;
font-family: sans-serif;
left: 38px;
font-size: 50px;
line-height: 50px;
overflow: hidden;
}
@keyframes move {
0% {
transform-origin: 100% 0;
transform: perspective(500px) rotateX(0deg);
}
100% {
background: black;
transform-origin: 0 0;
transform: perspective(500px) rotateX(280deg);
}
}
body {
background: #ddd;
}
JavaScript
document.getElementById("value").innerHTML = '00';
const onArrowBtnClick = (e) =>{
e.preventDefault();
var el = $('.move').clone();
$('.move').addClass('action');
el.appendTo($('.wrapper'));
setTimeout(function(){
$('.action').remove();
}, 1000);
}
let clicks = 0;
function setScoreOnBoard(score) {
setTimeout(function(){
document.getElementById("value").innerHTML = score;
}, 300);
}
function onUpArrowClick() {
clicks += 1;
let score;
if (clicks < 10 ) {
score = '0' + clicks;
} else {
score = clicks;
}
if (score > 0 && score < 99) {
setScoreOnBoard(score);
}
};
function onDownArrowClick() {
clicks -= 1;
let score;
if (clicks < 10 ) {
score = '0' + clicks;
} else {
score = clicks;
}
if (score >= 0 && score < 99) {
setScoreOnBoard(score);
}
};
function isScoreInTheRage() {
const actualScore = document.getElementById("value").innerHTML;
console.log(typeof actualScore);
return actualScore >= 0 && actualScore < 100
}
$('#triggerUp').on('click', (e) => {
if (isScoreInTheRage()) {
onUpArrowClick();
onArrowBtnClick(e);
}
});
$('#triggerDown').on('click', (e) => {
if (isScoreInTheRage()) {
onDownArrowClick();
onArrowBtnClick(e);
};
});