So what I have is a black bar that I want to move up and down continuously until I click the stop button. At the moment the black element doesn't move at all, and I'm sure the stop button wouldn't do it's job if it did. I would like the black bar to bounce up and down when it hits the edges of its container (the blue element). Eventually i would like to create a game where you have to stop the black bar inside the green zone and each time you do it correctly, it will get faster and the green area will move randomly, but one step at a time I guess. I am quite knew to java script and struggle to think of ways that I can make it work. My code currently stands at this
function load() {
var barMove = document.getElementById("small-bar");
var pos = 600;
var go = setInterval(start, 5);
function start() {
if (pos == 0) {
pos--;
barMove.style.top = pos + 'px';
} else if (pos == 600) {
pos++;
barMove.style.bottom + 'px';
}
}
}
body {
margin: 0;
padding: 0;
text-align: center;
}
#container {
position: relative;
background-color: brown;
height: 800px;
width: 800px;
margin: 0 auto;
margin-top: 75px;
}
#big-bar {
position: relative;
height: 600px;
width: 200px;
background: blue;
left: 300px;
top: 100px;
overflow: hidden;
border-radius: 20px 20px 20px 20px;
}
#medium-bar {
border-radius: 20px 20px 20px 20px;
z-index: 2;
position: absolute;
height: 100px;
width: 200px;
background: green;
top: 200px;
}
#small-bar {
border-radius: 20px 20px 20px 20px;
z-index: 3;
position: absolute;
height: 50px;
width: 200px;
background: black;
top: 600px;
}
#btn-go {
position: absolute;
top: 20px;
right: 600px;
padding: 5px 20px;
border-radius: 10px;
background: gold;
font-family: sans-serif;
font-size: 1.8em;
color: #424242;
}
#btn-stop {
position: absolute;
top: 20px;
right: 75px;
padding: 5px 20px;
border-radius: 10px;
background: red;
font-family: sans-serif;
font-size: 1.8em;
color: #424242;
}
<div id="container">
<div id="big-bar">
<div id="medium-bar"></div>
<div id="small-bar"></div>
</div>
<input id="btn-go" type="button" value="Go !" onclick="load()">
<input id="btn-stop" type="button" value="Stop !" onclick="clearInterval(go)">
</div>