<!--
* 다섯마리의 말들 경주시합 게임 만들기
- 다섯마리의 말을 그림으로 준비시킨다.
- 지정된 거리에 먼저 도착한 순서대로 등수를 매겨준다.
(콜백 함수: 최종수행이끝나고처리하는함수 를 이용하여 등수 구하기)
-->
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ex01_경주.html</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://kit.fontawesome.com/df66332deb.js" crossorigin="anonymous"></script>
<script>
'use strict';
$(function(){
let ani1 = Math.random()*5000 + 4000;
let ani2 = Math.random()*5000 + 4000;
let ani3 = Math.random()*5000 + 4000;
let ani4 = Math.random()*5000 + 4000;
let ani5 = Math.random()*5000 + 4000;
// let sw = !$("img").data("sw");
$("#startBtn").on("click", function(){
// function changeImage() {
// $("img").data("sw",sw);
// if(sw == true) $("img.horseImg1").attr("src","horse2.png");
// else $("img.horseImg2").attr("src","horse1.png");
// if($(".horseImg1").visible) $(".horseImg1").attr("src","horse2.png");
// else $(".horseImg2").attr("src","horse2.png");
// }
// setTimeout("changeImage()",200);
$("#horse1").animate({
marginLeft: "500px"
},ani1, function(){ranking("번개",ani1)});
$("#horse2").animate({
marginLeft: "500px"
},ani2, function(){ranking("태양",ani2)});
$("#horse3").animate({
marginLeft: "500px"
},ani3, function(){ranking("청룡",ani3)});
$("#horse4").animate({
marginLeft: "500px"
},ani4, function(){ranking("스톰",ani4)});
$("#horse5").animate({
marginLeft: "500px"
},ani5, function(){ranking("플래시",ani5)});
});
});
let cnt = 0;
let str = '';
function ranking(horse, time){
cnt++;
time = time / 1000 +"";
time = time.substring(0,4);
str += `${cnt}등: ${horse} (시간: ${time}초)<br/>`;
demo.innerHTML = str;
}
</script>
</head>
<body>
<p><br/></p>
<div class="container">
<button id="startBtn" class="btn btn-primary"><i class="fa-solid fa-play"></i> 출발</button>
<hr/>
<div id="horse1"><img src="horse1.png" class="horseImg1" width="100px"/>번개</div>
<div id="horse2"><img src="horse2.png" class="horseImg2" width="110px"/>태양</div>
<div id="horse3"><img src="horse1.png" class="horseImg1" width="100px"/>청룡</div>
<div id="horse4"><img src="horse2.png" class="horseImg2" width="110px"/>스톰</div>
<div id="horse5"><img src="horse1.png" class="horseImg1" width="100px"/>플래시</div>
<div id="demo"></div>
</div>
</body>
</html>
첫댓글 콜백함수처리로 랭킹순위구하기 좋습니다.
나머지도 꼭 해보세요. 수고하셨습니다.