Как сделать анимацию с помощью JavaScript
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Tutorial</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button onclick="myMove()">Animation</button>
<div id="myContainer">
<div id="myAnimation">
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
#myContainer {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#myAnimation {
width: 50px;
height: 50px;
position: absolute;
background: red;
}
JS
function myMove() {
'use strict';
var elem = document.getElementById("myAnimation"),
pos = 0;
function frame() {
var id = setInterval(frame, 10);
if (pos === 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Tutorial</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button onclick="myMove()">Animation</button>
<div id="myContainer">
<div id="myAnimation">
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
#myContainer {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#myAnimation {
width: 50px;
height: 50px;
position: absolute;
background: red;
}
JS
function myMove() {
'use strict';
var elem = document.getElementById("myAnimation"),
pos = 0;
function frame() {
var id = setInterval(frame, 10);
if (pos === 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
}
Комментарии
Отправить комментарий