<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
window.onload = function() {
var FSM = {
show1: {
clickBtn: function (key) {
change.call(this,key);
}
},
show2: {
clickBtn: function (key) {
change.call(this,key);
}
},
show3: {
clickBtn: function (key) {
change.call(this,key);
}
},
show4: {
clickBtn: function (key) {
change.call(this,key);
}
}
};
var Ball = function () {
this.curentState = FSM.show1;
this.div = null;
};
Ball.prototype.init = function () {
var self = this;
this.div = document.getElementById('go');
document.body.onkeydown = function (event) {
var key = event.keyCode;
self.curentState.clickBtn.call(self,key);
}
};
function change(key){
var styles = window.getComputedStyle(this.div),
parentStyles = window.getComputedStyle(this.div.parentNode),
top = parseInt(styles.top),
left = parseInt(styles.left);
if(key === 40){
top += (top+parseInt(styles.height))<parseInt(parentStyles.height) ? 10 : 0;
this.div.style.top = top+'px';
this.curentState = FSM.show3;
}
if(key === 38){
top -= (top > 0 ? 10 : 0);
this.div.style.top = top+'px';
this.curentState = FSM.show4;
}
if(key === 37){
left -= (left > 0 ? 10 : 0);
this.div.style.left = left+'px';
this.curentState = FSM.show1;
}
if(key === 39){
this.curentState = FSM.show2;
left += (left+parseInt(styles.width))<parseInt(parentStyles.width) ? 10 : 0;
this.div.style.left = left+'px';
}
}
var a = new Ball();
a.init();
}
</script>
<style>
#div{
position: absolute;
width: 80%;
height: 80%;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
border: 1px solid darkcyan;
}
#go{
position:absolute;
width:50px;
height:50px;
left: 10px;
top:20px;
border:1px solid gray;
-webkit-border-radius : 50px;
-moz-border-radius: 50px;
border-radius: 50px;
background-image: radial-gradient(circle, white 5%, black 100%);
}
</style>
</head>
<body>
<div id="div">按下方向键移动方块
<div id="go"></div>
</div>
</body>
</html>
// 状态机
var FSM = {
off: {
buttonWasPressed: function() {
console.log("关灯");
this.button.innerHTML = "下一次按我是开灯"; // 这是Light上的属性!!!
this.currState = FSM.on; // 这是Light上的属性!!!
}
},
on: {
buttonWasPressed: function() {
console.log("开灯");
this.button.innerHTML = "下一次按我是关灯";
this.currState = FSM.off;
}
},
};
var Light = function() {
this.currState = FSM.off; // 设置当前状态
this.button = null;
};
Light.prototype.init = function() {
var button = document.createElement("button");
self = this;
button.innerHTML = "已关灯";
this.button = document.body.appendChild(button);
this.button.onclick = function() {
// 请求委托给FSM状态机
self.currState.buttonWasPressed.call(self);
}
}
var light = new Light();
light.init();
var delegate = function(client, delegation) {
return {
buttonWasPressed: function() {
return delegation.buttonWasPressed.apply(client, arguments);
}
};
};
// 状态机
var FSM = {
off: {
buttonWasPressed: function() {
console.log("关灯");
this.button.innerHTML = "下一次按我是开灯";
this.currState = this.onState;
}
},
on: {
buttonWasPressed: function() {
console.log("开灯");
this.button.innerHTML = "下一次按我是关灯";
this.currState = this.offState;
}
},
};
var Light = function() {
this.offState = delegate(this, FSM.off);
this.onState = delegate(this, FSM.on);
this.currState = this.offState; // 设置当前状态
this.button = null;
};
Light.prototype.init = function() {
var button = document.createElement("button");
self = this;
button.innerHTML = "已关灯";
this.button = document.body.appendChild(button);
this.button.onclick = function() {
// 请求委托给FSM状态机
self.currState.buttonWasPressed();
}
}
var light = new Light();
light.init();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有