I'm trying to change the value of the top
margin
when hitting the up arrow key the code seems right to me but i don't know why it wont work !
JavaScript
var playerPosition = 0;
window.onkeyup = function(e) {
var key = e.keyCode ? e.keyCode : e.which;
if(key = 38) {
playerPosition += 10;
} else if(key = 40) {
playerPosition -= 10;
}
document.getElementsByClassName('player').style.marginTop = playerPosition+".px";
}
html/CSS
.mainDiv {
display: block;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: black;
width: 600px;
height: 400px;
}
.player {
position: absolute;
background-color: #FFF;
width: 5px;
height: 70px;
margin: 20px 0 0 10px;
padding: 0 0 0 0;
}
.bar {
top: 30px;
height: 100%;
width: 5px;
border-style: dashed;
border-left: 5px;
border-color: #FFF;
position: fixed;
left: 50%;
}
.ai {
position: absolute;
right: 10px;
background-color: #FFF;
width: 5px;
height: 70px;
margin: 164px 0 0 10px;
padding: 0 0 0 0;
}
.ball {
position: absolute;
left: 50px;
bottom: 69px;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #FFF;
}
</style>
<script src="sprite.js" defer="defer"></script>
</head>
<body>
<div class="mainDiv">
<div class="player"></div>
<div class="bar"></div>
<div class="ai"></div>
<div class="ball"></div>
</div>
</body>
</html>