刚学习vue不久,今天试着用vue做了一个简单的loading进度条,对于vue的生命周期和钩子函数又有了新的理解,下面分享给大家,绝对入门级。

一、进度条原理

这个就很简单了,也是我们经常可以用到的,这里只做一个最简单的,页面刷新自动加载进度条。主要是让进度条的width不断增加至100%就可以啦~好了,进入正题。

二、jquery实现

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.bootcss.com/foundation/5.5.3/css/foundation.min.css" rel="external nofollow" rel="external nofollow" >
  <link rel="stylesheet" href="http://static.runoob.com/assets/foundation-icons/foundation-icons.css" rel="external nofollow" rel="external nofollow" >
  <title>jq进度条</title>

</head>

<body>
  <div id="app">
    <div class="progress round alert">
      <span class="meter" style="width:0%">0%</span>
    </div>
  </div>


  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js "></script>
  <script src="https://cdn.bootcss.com/foundation/5.5.3/js/foundation.min.js "></script>
  <script>
    $(function () {
      var html = $('.meter');
      var htmlW = $('.meter').val();
      var styleW = parseInt($('.meter').css('width'));
      var clearInt = setInterval(function () {
        styleW++;
        var styleWW = styleW + '%';
        html.css('width', styleWW);
        html.html(styleWW);
        if (styleW == 100) {
          clearInterval(clearInt);
        }
      }, 20)
    })
  </script>
</body>

</html> 

三、vue实现

<!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>vue进度条</title>
  <link rel="stylesheet" href="https://cdn.bootcss.com/foundation/5.5.3/css/foundation.min.css" rel="external nofollow" rel="external nofollow" >
  <link rel="stylesheet" href="http://static.runoob.com/assets/foundation-icons/foundation-icons.css" rel="external nofollow" rel="external nofollow" >
  <style>
    .bar{
      color: #fff;
      text-align: center;
    }
  </style>

</head>

<body>
  <div id="app">
    <div class="progress alert round">
      <span class="meter bar" :style="{width:proBar+'%',}" >{{proBar}}</span>
    </div>
  </div>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js "></script>
  <script src="https://cdn.bootcss.com/foundation/5.5.3/js/foundation.min.js "></script>
  <script>
    var app = new Vue({
      el: '#app',
      data: {
        proBar: 0,
      },
      created(){
        this.change();
      },
      methods: {
        change: function() {
            var clearInt = setInterval(function() {
              app.proBar++;
              console.log(app.prpBar);
              if (app.proBar == 100) {
                clearInterval(clearInt);
              }
            }, 20)

        }
      }
    })
  </script>

</body>

</html> 

对比两段代码显然vue要方便许多,因为是双向绑定,不用来回操作dom,就很简单省心啦。

注意问题:刚开始没有实现loading效果,主要是因为用了mounted钩子函数来调用change方法(还是对生命周期理解的不到位)。实现loading效果我们需要在页面加载时自动调用change方法,所以我们应该在数据全部初始化前就执行这一操作。mounted时期已经全部完成初始化,所以便不会成功。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

02-05 06:29