Chapter 7: Douglas dance
The starting point for the Animated Robot project from Chapter 7 of JavaScript for Kids For Dummies by Chris Minnick and Eva Holland.
by rosakaufman
HTML
<div id="robot">
<div id="head">
<div class="eye" id="righteye"></div>
<div class="eye" id="lefteye"></div>
<div id="nose"></div>
<div id="mouth"></div>
</div>
<div class="arm" id="rightarm"></div>
<div id="body">
<p>I <3 JS!</p>
</div>
<div class="arm" id="leftarm"></div>
</div>
CSS
body {
font-family: "Comic Sans MS", cursive, sans-serif;
}
p {
font-size: 3em;
}
.eye {
background-color: blue;
width: 30%;
height: 30%;
border-radius: 50%;
}
.arm {
background-color: #be2596;
position: absolute;
top: 35%;
width: 5%;
height: 40%;
}
#head {
width: 30%;
height: 30%;
border-radius: 15%;
background-color: #e03ab4;
position: absolute;
left: 33%;
top: 5%;
}
#righteye {
position: absolute;
left: 10%;
top: 10%;
}
#lefteye {
position: absolute;
left: 60%;
top: 10%;
}
#nose {
position: absolute;
left: 40%;
top: 40%;
width: 20%;
height: 20%;
background-color: black;
}
#mouth {
position: absolute;
width: 65%;
height: 15%;
left: 20%;
top: 70%;
background-color: #8d1e87;
}
#body {
position: absolute;
left: 25%;
top: 35%;
width: 45%;
height: 55%;
background-color: #9925be;
text-align: center;
padding-top: 30px;
}
#rightarm {
position: absolute;
left: 20%;
}
#leftarm {
position: absolute;
left: 70%;
width: 25%;
height: 10%;
}
JavaScript
document.getElementById("lefteye").style.backgroundColor = "#801ad9";
document.getElementById("head").style.transform = "rotate(-15deg)";
document.getElementById("mouth").style.borderRadius = "6px";
document.getElementById("righteye").style.border = "4px yellow dotted";
document.getElementById("leftarm").style.backgroundColor = "#ff00ff";
document.getElementById("body").style.color = "#ff0000";
document.getElementById("head").style.borderTop = "8px black solid";
document.getElementById("nose").style.borderRadius = "50%";
document.getElementById("rightarm").style.backgroundColor = "#2eb00b";
document.getElementById("mouth").style.color = "#8d1e87";
var rightEye = document.getElementById ("righteye");
var leftEye = document.getElementById("lefteye");
var leftArm = document.getElementById("leftarm");
rightEye.addEventListener("click", moveUpDown);
leftEye.addEventListener("click", moveUpDown);
leftArm.addEventListener("click", moveRightLeft);
function moveUpDown(e){
var robotPart = e.target;
var top = 0;
var id = setInterval(frame, 10) //draw every 10 ms
function frame(){
robotPart.style.top = top + '%';
top++;
if(top === 20){
clearInterval(id);
}
}
}