Vue simple lottery demo

Simple lucky draw Demo

vue.js file


<!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>
  • notes:
  • v-cloak [It will be rendered after all data is loaded]
  • v-bind: [can load data in vue] abbreviated as [:]
  • Abbreviation of v-on binding event[@]

Take a few notes so you can read them later.

Guess you like

Origin blog.csdn.net/qq_44111597/article/details/109114073