Vue small practice 01

  • Red, yellow, and blue buttons, and a rectangular box of a 200X200px, clicking different buttons, box color will be switched to a specified color
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>

</head>
<body>
<div id="d1">

   <p :style="myStyle"></p>

   <button :style="{backgroundColor: bgc1}" @click="f1">按钮一</button>
   <button :style="{backgroundColor: bgc2}" @click="f2">按钮二</button>
   <button :style="{backgroundColor: bgc3}" @click="f3">按钮三</button>
</div>

<script src="vue/vue.js"></script>
<script>
   new Vue({
       el: '#d1',
       data: {
           bgc1: 'red',
           bgc2: 'yellow',
           bgc3: 'blue',
           myStyle: {
               width: '200px',
               height: '200px',
               backgroundColor: 'black'
           }
       },
       methods: {
           f1() {
               this.myStyle.backgroundColor = this.bgc1
           },
           f2() {
               this.myStyle.backgroundColor = this.bgc2
           },
           f3() {
               this.myStyle.backgroundColor = this.bgc3
           },
       }
   })
</script>


</body>
</html>
  • 200X200px of a rectangular box, click on the box itself, record clicks, 1 box turns pink, 2 to green, to blue three times, four times to return to pink, and so on
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div id="d1">

    <p :style="myStyle" @click="f1">{{ counter }}</p>


</div>

<script src="vue/vue.js"></script>
<script>
    new Vue({
        el: '#d1',
        data: {
            counter: 0,
            bgc1: 'pink',
            bgc2: 'green',
            bgc3: 'cyan',
            myStyle: {
                width: '200px',
                height: '200px',
                backgroundColor: 'black'
            }
        },
        methods: {
            f1() {
                this.counter += 1;
                if (this.counter % 3 === 1) {
                    this.myStyle.backgroundColor = this.bgc1
                }else if (this.counter % 3 === 2) {
                    this.myStyle.backgroundColor = this.bgc2
                }else {
                    this.myStyle.backgroundColor = this.bgc3
                }
            }
        }
    })
</script>


</body>
</html>

Guess you like

Origin www.cnblogs.com/bigb/p/12052272.html