admin管理员组文章数量:1400744
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>
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>
Share
Improve this question
asked Dec 27, 2014 at 2:41
ZEEZEE
5,8794 gold badges39 silver badges54 bronze badges
2
- possible duplicate of GetElementsByClassName Not Working As Expected – user663031 Commented Dec 27, 2014 at 7:52
- See also stackoverflow./questions/24292561/…, stackoverflow./questions/19289907/…, stackoverflow./questions/10693845/…, etc. – user663031 Commented Dec 27, 2014 at 7:54
4 Answers
Reset to default 2document.getElementsByClassName('player')
returns a NodeList
of elements (array-like) that have the class player
. You need to loop through the list and apply the style changes to each:
var players = document.getElementsByClassName('player');
for(var i = 0; i < players.length; i++)
players[i].style.marginTop = playerPosition+"px";
Or I guess if you only have one, apply it to the 0th element.
fiddle
Function is called "getElements[...]", plural, so it returns an array of HTML elements with class name provided. Moreover in your conditional statements you used assignment operator (=) instead of parison operator (==). jsfiddle
document.getElementsByClassName('player')[0].style.marginTop = playerPosition+"px";
getElementsByClassName returns a HTMLCollection. So, you have to specify the element to work.
for example:
document.getElementsByClassName('player')[0].style.marginTop = playerPosition+".px";
document.getElementById("MyElement").classList.add('MyClass');
document.getElementById("MyElement").classList.remove('MyClass');
if ( document.getElementById("MyElement").classList.contains('MyClass') )
document.getElementById("MyElement").classList.toggle('MyClass');
本文标签: htmlchange the margin using variable with JavaScriptStack Overflow
版权声明:本文标题:html - change the margin using variable with JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744277938a2598507.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论