一,首先创建一个项目,并引入Vue组件。
<script src="js/vue.js"></script>
二,整体页面布局
<div id="app">
<div class="content">
<div class="box"
:style=
"{
top:topValue+'px',
left:leftValue+'px'
}"
>
</div>
<div class="stop"></div>
</div>
</div>
</body>
三,编写样式文件,添加背景图片。
<style>
.content{
height: 800px;
width: 800px;
margin: 60px auto;
background-image: url(./img/cc.png);
border: 1px solid black;
}
.box{
height: 100px;
width: 100px;
background-image: url(./img/xx.png);
position: relative;
}
.stop{
width: 100px;
height: 100px;
background-color: #d299c2;
position: relative;
top: 300px;
left: 300px;
}
</style>
四,Vue代码,用if判断语句来设置上下左右边界和墙壁的上下左右边界,通过判断使箱子移动,最后给整个页面添加事件监听。
<script>
new Vue({
el: "#app",
data: {
topValue: 200,
leftValue: 400,
},
methods:{
move(event) {
switch (event.keyCode) {
//上
case 38:
if (this.topValue == 0 || this.topValue == 500 && this.leftValue == 300) {
break;
}
console.log(this.topValue,this.leftValue)
this.topValue -= 100;
break;
//下
case 40:
if (this.topValue == 700 || this.topValue == 300 && this.leftValue == 300) {
break;
}
console.log(this.topValue,this.leftValue)
this.topValue += 100;
break;
//左
case 37:
if (this.leftValue == 0 || this.topValue == 400 && this.leftValue == 400) {
break;
}
console.log(this.topValue,this.leftValue)
this.leftValue -= 100;
break;
//右
case 39:
if (this.leftValue == 700 || this.topValue == 400 && this.leftValue == 200) {
break;
}
console.log(this.topValue,this.leftValue)
this.leftValue += 100;
break;
}
}
},
//给整个页面添加事件监听
mounted:function () {
document.addEventListener("keydown",this.move)
}
})
</script>
五,完整代码及效果展示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>推箱子</title>
<script src="js/vue.js"></script>
<style>
.content{
height: 800px;
width: 800px;
margin: 60px auto;
background-image: url(./img/cc.png);
border: 1px solid black;
}
.box{
height: 100px;
width: 100px;
background-image: url(./img/xx.png);
position: relative;
}
.stop{
width: 100px;
height: 100px;
background-color: #d299c2;
position: relative;
top: 300px;
left: 300px;
}
</style>
</head>
<body>
<div id="app">
<div class="content">
<div class="box"
:style=
"{
top:topValue+'px',
left:leftValue+'px'
}"
>
</div>
<div class="stop"></div>
</div>
</div>
</body>
<script>
new Vue({
el: "#app",
data: {
topValue: 200,
leftValue: 400,
},
methods:{
move(event) {
switch (event.keyCode) {
//上
case 38:
if (this.topValue == 0 || this.topValue == 500 && this.leftValue == 300) {
break;
}
console.log(this.topValue,this.leftValue)
this.topValue -= 100;
break;
//下
case 40:
if (this.topValue == 700 || this.topValue == 300 && this.leftValue == 300) {
break;
}
console.log(this.topValue,this.leftValue)
this.topValue += 100;
break;
//左
case 37:
if (this.leftValue == 0 || this.topValue == 400 && this.leftValue == 400) {
break;
}
console.log(this.topValue,this.leftValue)
this.leftValue -= 100;
break;
//右
case 39:
if (this.leftValue == 700 || this.topValue == 400 && this.leftValue == 200) {
break;
}
console.log(this.topValue,this.leftValue)
this.leftValue += 100;
break;
}
}
},
//给整个页面添加事件监听
mounted:function () {
document.addEventListener("keydown",this.move)
}
})
</script>
</html>