Demonstração de sorteio simples
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>抽奖Demo</title>
<script src="vue.js"></script>
</head>
<body>
<div id="id01">
<p>{
{score}}</p>
<p>{
{level}}</p>
<input type="button" value="开始抽奖" @click="start()">
<input type="button" value="停止抽奖" @click="stop()">
</div>
</body>
</html>
<script>
var vm = new Vue({
el: "#id01",
data: {
score: 0,
level: null,
intervalIndex: null
},
methods: {
start() {
if (this.intervalIndex == null) {
this.intervalIndex = setInterval(() => {
// Math.random() 生成 0-1 之间的随机数【但是小数位有点长】
// parseInt() 转换成整型
this.score = parseInt(Math.random() * 100);
switch (true) {
case this.score <= 50:
this.level = "幸运奖"
break;
case this.score <= 80 && this.score > 50:
this.level = "三等奖"
break;
case this.score <= 90 && this.score > 80:
this.level = "二等奖"
break;
case this.score > 90:
this.level = "一等奖"
break;
}
}, 50);
}
},
stop() {
clearInterval(this.intervalIndex);
this.intervalIndex = null
}
},
})
</script>
- notas:
- v-cloak [será renderizado quando todos os dados forem carregados]
- v-bind: [pode carregar dados em vue] abreviado como [:]
- Abreviação de evento de ligação v-on [@]
Faça algumas anotações para lê-las mais tarde.