zashii-1434

Stats of My Life(おいしい人生を味わうために、コツコツとチャレンジしたことを書くブログ)

JavaScript-オブジェクト

自分用メモ。

 

<!DOCTYPE html>
<html>
<head>
<style>
.div {
position: absolute;
width:60px;
height: 25px;
color: green;
top:50px;
}
</style>

<script>
var ufo;
function init() {
window.addEventListener("keydown",keydown);
ufo = new UFO(document.getElementById("ufo"),100);
}
function keydown(e) {
if (e.keydown == 37) {
ufo.moveLeft();
} else if (e.keydown == 39) {
ufo.moveRight();
}
}
function UFO(_elem, _xpos) {
this.elem = _elem;
this.xpos = _xpos;
this.moveLeft = function () {

this.xpos -= 10;
this.elem.style.left = this.xpos + "px";
}
this.moveRight = function () {
this.xpos += 10;
this.elem.style.left = this.xpos + "px";

}
this.elem.style.left = this.xpos + "px";
}

</script>
</head>
<body onload="init()">
<div id ="ufo">(000)</div>
 
</body>
</html>