当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue实现简单loading进度条

vue实现简单loading进度条

2018年08月08日  | 移动技术网IT编程  | 我要评论

北京德科岛金,特朗普女儿伊万卡,戚薇动态图片

刚学习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时期已经全部完成初始化,所以便不会成功。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网