JSFiddle - React, Tailwind, and code Playground
by Qwerty_Wasd
HTML
<div class="workSpace">
<div class="box" id="boxOne">1</div>
<div class="box" id="boxTwo">2</div>
<div class="buttons">
<div class="button" id="stopOne">"Остановить 1" НЕ работает</div>
<div class="button" id="stopTwo">"Остановить 2" НЕ работает</div>
<div class="button" id="stopAll">"Остановить все" работает</div>
</div>
</div>
CSS
html, body{
margin: 0px 0px 0px 0px;
width: 100%;
height: 100%;
overflow: hidden;
}
.workSpace{
width: 100%;
height: 100%;
background: #eee5ff;
font-family: comic sans ms;
overflow: hidden;
}
.buttons{
position: relative;
top: 250px;
}
.button{
margin-left: 10px;
padding: 10px 10px 10px 10px;
cursor: pointer;
letter-spacing: 1px;
border: 2px solid #eee5ff;
border-radius: 7px;
color: #fff;
display: inline-block;
text-align: center;
}
.button:hover{
/* background: #eee5ff; */
border: 2px solid #000000;
}
.box{
text-align: center;
color: #fff;
}
#boxOne{
width: 50px;
height: 50px;
font-size: 33px;
overflow: hidden;
position: absolute;
background: #795548;
left: 180px;
top: 20px;
}
#boxTwo{
width: 100px;
height: 100px;
font-size: 67px;
overflow: hidden;
position: absolute;
background: #009688;
left: 180px;
top: 90px;
}
#stopOne{
width: 140px;
background: #795548;
}
#stopTwo{
width: 140px;
background: #009688;
}
#stopAll{
width: 150px;
background: linear-gradient(to top left, #009688, #795548);
}
#stopOne:active{
background: red;
}
#stopTwo:active{
background: red;
}
#stopAll:active{
background: #00af00;
}
JavaScript
var coord = 0, interval;
var box = document.querySelectorAll('.box');
moveRight();
function moveRight(){
interval = requestAnimationFrame(moveRight);
coord += 1;
for(i=0; i<box.length; i++){
box[i].style.left = coord + 'px';
}
}
document.querySelector('#stopAll').onclick = ()=> {
cancelAnimationFrame(interval);
}
document.querySelector('#stopOne').onclick = ()=> {
// как их можно остановить по отдельности
}
document.querySelector('#stopTwo').onclick = ()=> {
// как их можно остановить по отдельности
}