angularjs實(shí)現(xiàn)柱狀圖動(dòng)態(tài)加載的示例
一 準(zhǔn)備工作
1.引用文件
下面鏈接中有一個(gè)jquery.js文件,請(qǐng)?jiān)趇ndex.html中引用。
2.新建文件
新建一個(gè)js文件,編寫指令。這也是我第一次寫指令,指令可擴(kuò)展性強(qiáng),還很方便,當(dāng)項(xiàng)目中重復(fù)使用的一些效果時(shí)可以通過指令來減少冗余的代碼。
二 代碼編寫
/** * Created by xiehan on 2017/12/8. * 柱狀圖動(dòng)態(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:"測(cè)試1", NUM:30, RATE:30 }, { NAME:"測(cè)試2", NUM:25, RATE:25 }, { NAME:"測(cè)試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; }
三 效果圖
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript canvas基于數(shù)組生成柱狀圖代碼實(shí)例
- JS highcharts動(dòng)態(tài)柱狀圖原理及實(shí)現(xiàn)
- JavaScript實(shí)現(xiàn)的DOM繪制柱狀圖效果示例
- 利用D3.js實(shí)現(xiàn)最簡(jiǎn)單的柱狀圖示例代碼
- D3.js實(shí)現(xiàn)柱狀圖的方法詳解
- jQuery.Highcharts.js繪制柱狀圖餅狀圖曲線圖
- JavaScript根據(jù)數(shù)據(jù)生成百分比圖和柱狀圖的實(shí)例代碼
- Javascript實(shí)時(shí)柱狀圖實(shí)現(xiàn)代碼
- JS使用canvas技術(shù)模仿echarts柱狀圖
相關(guān)文章
Angular4綁定html內(nèi)容出現(xiàn)警告的處理方法
這篇文章主要給大家介紹了關(guān)于Angular4綁定html內(nèi)容出現(xiàn)警告的處理方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11ionic+AngularJs實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)按鈕
本篇文章主要介紹了ionic+AngularJs實(shí)現(xiàn)獲取驗(yàn)證碼倒計(jì)時(shí)按鈕,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04在Angular中使用NgTemplateOutlet創(chuàng)建可重用組件的流程步驟
在 Angular 中,使用 NgTemplateOutlet 而不是創(chuàng)建特定組件,可以使組件在不修改組件本身的情況下輕松修改為各種用例,在本文中,您將接受一個(gè)現(xiàn)有組件并重寫它以使用 NgTemplateOutlet,需要的朋友可以參考下2024-03-03AngularJS基礎(chǔ) ng-mouseover 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-mouseover 指令,這里幫大家整理了AngularJS 指令的基礎(chǔ)知識(shí),并附代碼示例,有興趣的小伙伴可以參考下2016-08-08關(guān)于Angularjs中自定義指令一些有價(jià)值的細(xì)節(jié)和技巧小結(jié)
這篇文章主要介紹了關(guān)于Angularjs中自定義指令一些有價(jià)值的細(xì)節(jié)和技巧小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04AngularJS 中使用Swiper制作滾動(dòng)圖不能滑動(dòng)的解決方法
Swiper是目前較為流行的移動(dòng)端觸摸滑動(dòng)插件,因?yàn)槠浜?jiǎn)單好用易上手,受到很多前端開發(fā)者的歡迎。這篇文章主要介紹了AngularJS 中使用Swiper制作滾動(dòng)圖不能滑動(dòng)的解決方法,需要的朋友可以參考下2016-11-11AngularJS 前臺(tái)分頁實(shí)現(xiàn)的示例代碼
本篇文章主要介紹了AngularJS 前臺(tái)分頁實(shí)現(xiàn)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06