vue--实现跑马灯效果


<div id="app">
        <input type="button" value="开始" @click="lang">
        <input type="button" value="挺" @click="clear">
        <p>{{msg}}</p>
    </div>

  

 <script>
        var vm = new Vue({
            el: "#app",
            data: {
                msg: "我哈你丹江口安居客耙齿菌",
                id: null,
            },
            methods: {
                lang: function () {
                    if (!this.id) {//值为true指向下面这一段代码
                        this.id=setInterval(() => { 
                            var start = this.msg.substring(0, 1);
                            var end = this.msg.substring(1);
                            this.msg = end + start; //是最后一段拼接上前面的一段

                            //当开启定时器后 id就改变为了2哦
                            console.log(this.id) //2
                        }, 400);
                    } else {
                    
                    }
                },
                clear: function () {
                   clearInterval(this.id);  //清除定时器后  id仍然为2
                   console.log("清除前id为"+this.id);
                   this.id=null;
                }
            },

        })
    </script>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM