Vue.js01:跑馬燈效果

<!DOCTYPE html>
<html lang="en">
<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>Document</title>
    <script src="./vue.min.js"></script>
</head>
<body>
    <div id="app">
        <!-- @ 用於綁定事件 -->
        <input type="button" value="跑起來" @click="start">
        <input type="button" value="停下來" @click="stop">
        <!-- 插值表達式,展現data屬性數據 -->
        <h3>{{ msg }}</h3>
    </div>
</body>
<script>
    let vm = new Vue({
        // 規定操做的前端元素,id值爲app的元素
        el: '#app',
        data:{
            msg: '恕我直言,在座的各位都是垃圾...',
            // 
            myInterval: null
        },
        methods:{
            start:function(){
                console.log('跑起來')
                if(this.myInterval){
                    return
                }
                // => 的做用,我還朦朦朧朧的,大概意思就是使下面的this可以訪問上面的data中的數據
                this.myInterval = setInterval(() => {
                    // 綁定字符串中的第一個元素
                    let start = this.msg.substring(0, 1)
                    // 綁定字符串中除第一個元素外的其餘元素
                    let end = this.msg.substring(1)
                    // 進行新的字符串拼接並賦值
                    this.msg = end + start
                }, 200)
            },
            stop(){
                console.log('停下來')
                // clearInterval 用來終止動做
                clearInterval(this.myInterval)
                this.myInterval = null
            }
        }
    })
</script>
</html>
相關文章
相關標籤/搜索