index.html
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Анимация</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>
style.css
#myContainer{
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#myAnimation {
width: 50px;
height: 50px;
position: absolute;
background: red;
}
script.js
function myMove() {
var elem = document.getElementById("myAnimation");
var pos = 0; // позиция
var id = setInterval(frame, 10);
function frame() {
if(pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
}
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Анимация</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>
style.css
#myContainer{
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#myAnimation {
width: 50px;
height: 50px;
position: absolute;
background: red;
}
script.js
function myMove() {
var elem = document.getElementById("myAnimation");
var pos = 0; // позиция
var id = setInterval(frame, 10);
function frame() {
if(pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
}
Комментариев нет:
Отправить комментарий