走马灯

作者: lucky_yao | 来源:发表于2020-10-20 07:49 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    
        <head>
            <meta charset="UTF-8">
            <title>Document</title>
            <script src="js/vue.min.js"></script>
        </head>
    
        <body>
            <div id="app">
                <button @click="show">浪起来</button>
                <button @click="stop">别浪</button>
                <p>{{msg}}</p>
            </div>
    
            <script>
                // 创建 Vue 实例,得到 ViewModel
                var vm = new Vue({
                    el: '#app',
                    data: {
                        msg: '猥琐发育,别浪~~~!',
                        timer: null
                    },
                    methods: {
                        show() {
                            if (this.timer == null) {
                                // var that = this; //this指代的是我们的vue
                                // that.timer = setInterval(function() { //在定时器中this指的是window
                                //  let strat = that.msg.substring(0, 1);
                                //  let end = that.msg.substring(1);
                                //  that.msg = end + strat;
                                // }, 500)
                                this.timer = setInterval(() => {
                                    let strat = this.msg.substring(0, 1);
                                    let end = this.msg.substring(1);
                                    this.msg = end + strat;
                                },500)
                            }
    
                        },
                        stop() {
                            clearInterval(this.timer);
                            this.timer = null;
                        }
                    }
                });
            </script>
        </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:走马灯

          本文链接:https://www.haomeiwen.com/subject/jbfqpktx.html