Vue实现简单的跑马灯效果

请皇上批阅

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>跑马灯</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <input type="button" value="寻找" @click="search">
        <input type="button" value="停止" @click="stop">
        <h3>{{msg}}</h3>
    </div>
<script>
    //操作 Vue 中的属性和方法 使用 this.
    var vm = new Vue({
        el:'#app',
        data: {
            msg: '众里寻你!!!你却不知道~~~!',
            intInterval: null
        },
        methods: {
            search(){
                if (this.intInteval != null)
                    return;
                // 这里使用了箭头函数,所以 this 不会发生冲突
                this.intInteval = setInterval( () => {
                    var start = this.msg.substring(0, 1);
                    var end = this.msg.substring(1);
                    this.msg = end + start;
                }, 500)
            },
            stop(){
                clearInterval(this.intInteval);
                this.intInteval = null;
            }
        }
    });
</script>
</body>
</html>
发布了62 篇原创文章 · 获赞 0 · 访问量 1247

猜你喜欢

转载自blog.csdn.net/weixin_45616246/article/details/105319854