JSFiddle - React, Tailwind, and code Playground
by Mariya_Korotkova
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="wrapper">
<div class="cosmos">
<div class="planet">
<img src="planet.png" alt="planet">
</div>
<div class="rocket">
<img src="rocket.png" alt="rocket">
</div>
</div>
<div class="button">
<button class="btn">Поехали!</button>
</div>
</div>
<script src='script.js'></script>
</body>
</html>
CSS
.wrapper {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
display: flex;
align-items: center;
/* align-content: center; */
justify-content: center;
flex-direction: column;
/* overflow: auto; */
}
.cosmos {
width: 479px;
height: 308px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
.planet img {
width: 200px;
height: 200px;
display: block;
}
.rocket img {
width: 50px;
height: 50px;
}
button {
margin: 5px;
padding: 5px 10px;
}
JavaScript
let btn = document.querySelector('button'),
rocket = document.querySelector('.rocket'),
cosmos = document.querySelector('.cosmos');
function myAnimation() {
// requestAnimationFrame(myAnimation);
let pos = rocket.getBoundingClientRect().x;
// let pos = 0;
console.log(pos);
let id = setInterval(frame, 10);
function frame() {
if (pos == 720) {
clearInterval(id);
} else {
pos++;
// rocket.style.top = pos + 'px';
rocket.style.left = pos + 'px';
}
}
}
// let posCosmos = cosmos.getBoundingClientRect();
// console.log(posCosmos);
btn.addEventListener('click', myAnimation);
// let width = cosmos.clientWidth,
// height = cosmos.clientHeight;
// console.log(width);
// console.log(height);
// console.log(rocket);