跑马灯效果 --Y.

  1. HTML结构:
    <div id="app">

        <p>{{info}}</p>

        <input type="button" value="开启" v-on:click="go">

        <input type="button" value="停止" v-on:click="stop">

      </div>
  1. Vue实例:
        // 创建 Vue 实例,得到 ViewModel

        var vm = new Vue({

          el: '#app',

          data: {

            info: '猥琐发育,别浪~!',

            intervalId: null

          },

          methods: {

            go() {

              // 如果当前有定时器在运行,则直接return

              if (this.intervalId != null) {

                return;

              }

              // 开始定时器

              this.intervalId = setInterval(() => {

                this.info = this.info.substring(1) + this.info.substring(0, 1);

              }, 500);

            },

            stop() {

              clearInterval(this.intervalId);

            }

          }

        });

猜你喜欢

转载自blog.csdn.net/weixin_42575354/article/details/81537629