效果图
点击Punch蓝条减小 点Restart回满
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script>
<style>
#bag {
width: 200px;
height: 500px;
margin: 0 auto;
background: url(./static/bag.jpg) center no-repeat;
background-size: 80%;
}
#bag-healthy {
width: 200px;
border: 2px #000 solid;
margin: 0 auto 20px auto;
}
#bag-healthy div {
height: 20px;
background-color: cornflowerblue;
}
#controls {
width: 120px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="app">
<!-- bag image -->
<div id="bag" :class="{burst: ended}"></div>
<!-- bag health -->
<div id="bag-healthy">
<div :style="{width: health + '%'}"></div>
</div>
<!-- game control -->
<div id="controls">
<button @click="punch" v-show="!ended">Punch</button>
<button @click="restart">Restart</button>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data:{
health: 100,
ended: false
},
methods: {
punch() {
this.health --
if (this.health <=0) {
this.ended = true
}
},
restart() {
this.health = 100
}
},
})
</script>
</body>
</html>