angularjs實(shí)現(xiàn)柱狀圖動態(tài)加載

一 準(zhǔn)備工作

1.引用文件

下面鏈接中有一個(gè)jquery.js文件,請?jiān)趇ndex.html中引用。
鏈接:https://pan.baidu.com/s/14B9G34ZPoS0_P1lVJwvhWw
提取碼:laze
復(fù)制這段內(nèi)容后打開百度網(wǎng)盤手機(jī)App,操作更方便哦

2.新建文件

新建一個(gè)js文件,編寫指令。這也是我第一次寫指令,指令可擴(kuò)展性強(qiáng),還很方便,當(dāng)項(xiàng)目中重復(fù)使用的一些效果時(shí)可以通過指令來減少冗余的代碼。

二 代碼編寫

/**
 * Created by xiehan on 2017/12/8.
 * 柱狀圖動態(tài)加載指令
 */
angular.module('studyApp.directives')
  .directive('progressPer', function ($compile,$timeout) {
    return {
      restrict: 'AE',
      scope: {
        progressData: '='
      },
      template: ' <div class="progress-main" ng-repeat="item in progressData">'+
      '<div class="progress-data">'+
      '<span>{{item.name}}</span>'+
      '<div class="skillbar clearfix " data-percent={{item.width}}>'+
        '<div class="skillbar-bar"></div>'+
          '<div class="skill-bar-percent">{{item.sum}}</div>'+
        '</div>'+
      '</div>'+
      '<p class="progress-rate">{{item.percent}}</p>'+
      '</div>',
      replace: true,
      transclude: true,
      link: function (scope, element, attrs) {
        $compile(element.contents())(scope.$new());

        $timeout(function() {
          jQuery('.skillbar').each(function(){
            jQuery(this).find('.skillbar-bar').animate({
              width:jQuery(this).attr('data-percent')
            },1000);
          });
        });
      }
    }
  });

/**
 * Created by xiehan on 2017/11/29.
 * controller文件
 */
angular.module('studyApp.controllers')

  .controller('ProgressCtrl', function ($scope, $rootScope, $ionicHistory,$timeout,$location) {
    $scope.title = '進(jìn)度條效果';

    $scope.goBack = function () {
      $ionicHistory.goBack();
    };

    var dataInfo=[
      {
        NAME:"測試1",
        NUM:30,
        RATE:30
      },
      {
        NAME:"測試2",
        NUM:25,
        RATE:25
      },
      {
        NAME:"測試3",
        NUM:45,
        RATE:45
      }
    ];

    handleTabData(dataInfo);

    function handleTabData(data){
      var widthData=[];
      for(var i = 0;i<data.length;i++){
        widthData.push({
          width:data[i].RATE+'%',       //進(jìn)度條百分比
          name:data[i].NAME,            //標(biāo)題
          sum:data[i].NUM,              //數(shù)量
          percent:data[i].RATE+'%'});   //百分比
      }
      $scope.handleDataInfo = widthData;
     //不使用指令加上下面的代碼
      // $timeout(function() {
      //   jQuery('.skillbar').each(function(){
      //     jQuery(this).find('.skillbar-bar').animate({
      //       width:jQuery(this).attr('data-percent')
      //     },1000);
      //   });
      // });
    }
  });

<ion-item>不使用指令</ion-item>
    <div class="progress-main" ng-repeat="item in handleDataInfo">
      <div class="progress-data">
        <span>{{item.name}}</span>
        <div class="skillbar clearfix " data-percent={{item.width}}>
          <div class="skillbar-bar"></div>
          <div class="skill-bar-percent">{{item.sum}}</div>
        </div>
      </div>
      <p class="progress-rate">{{item.percent}}</p>
    </div>

    <ion-item>使用指令</ion-item>
    <progress-per progress-data="handleDataInfo"></progress-per>

/***************進(jìn)度條樣式css********/
.skillbar {
  position: relative;
  display: block;
  margin-bottom: 15px;
  width: 100%;
  background: #eee;  /**背景顏色**/
  height: 35px;
  border-radius: 3px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  -webkit-transition: 0.4s linear;
  -moz-transition: 0.4s linear;
  -ms-transition: 0.4s linear;
  -o-transition: 0.4s linear;
  transition: 0.4s linear;
  -webkit-transition-property: width, background-color;
  -moz-transition-property: width, background-color;
  -ms-transition-property: width, background-color;
  -o-transition-property: width, background-color;
  transition-property: width, background-color;
}

.skillbar-bar {
  height: 35px;
  width: 0px;
  background: #50d2c2;
  border-radius: 3px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
}

.skill-bar-percent {
  position: absolute;
  right: 10px;
  top: 0;
  font-size: 11px;
  height: 35px;
  line-height: 35px;
  color: #ffffff;
  color: rgba(0, 0, 0, 0.4);
}

.progress-main{
  display: flex;
  display: -webkit-flex;
  align-items: center;
  -webkit-align-items: center;
  justify-content: center;
  -webkit-justify-content: center;
  margin-top: 10px;
}

.progress-data{
  margin-left: 5%;
  width: 100%;
  float: left;
}

.progress-rate{
  float: right;
  width: 20%;
  line-height: 35px;
  margin-left: 5%;
  margin-top: 10px;
}

三 效果圖

效果圖.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,157評論 25 708
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,740評論 18 399
  • 在線閱讀 http://interview.poetries.top[http://interview.poetr...
    前端進(jìn)階之旅閱讀 115,568評論 24 450
  • 男人的幸福,不是吃的好,穿的好,住的好就叫幸福! 是擁有一個(gè)愛自己懂自己的人! 錢,是物質(zhì)的需求,幸福,是精神上的...
    彬哥說話閱讀 229評論 0 1
  • 如果花開了,就歡喜;如果花落了,就放棄。陪你在路上滿心歡喜是因?yàn)轱L(fēng)景,不是因?yàn)槟恪?之前說在杭州的生活和工作圈子太...
    這一只果是大小姐閱讀 288評論 0 0

友情鏈接更多精彩內(nèi)容