VUEのケース-ランニングライト効果

11767 ワード

Vueの課程の中で学んだ1つの小さいケースを独学して、馬の明かりの効果を走ります
DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Documenttitle>
    
    <script src="./lib/vue-2.4.0.js">script>
head>

<body>
    
    <div id="app">
        <input type="button" value="   " @click="run">
        <input type="button" value="   " @click="stop">

        <h4>{{ msg }}h4>
    div>

    <script>
        //   :  vm    ,       data     ,        methods     ,    this.        this.         ,   this,       new     vm     
        var vm = new Vue({
            el: '#app',
            data: {
                msg: '    ,  ~~!',
                setIntervalId: null
            },
            methods: {
                run() {
                    if (this.intervalId != null) return;

                    //            this   
                    this.intervalId = setInterval(() => {
                        //           
                        var start = this.msg.substring(0, 1);
                        //          
                        var end = this.msg.substring(1);
                        //            ,     this.msg
                        this.msg = end + start;
                    }, 400);

                    //   :vm  ,         data         ,         ,           , data         ,(  :          ,           DOM  )
                },
                stop() { //      
                    clearInterval(this.intervalId);
                    //           ,      intervalId    null
                    this.intervalId = null;
                }
            }
        });


        //
        // 1. “   ”          
        // 2.           ,          :   msg    ,         substring            ,          ,          
        // 3.         ,       ,   2      ,         
    script>
body>

html>