本文推薦給大家的是使用angularjs實現整合微信新推出的UI(weui)的全部過程,有相同需求的小夥伴可以參考下
引子
不久前,微信推出了自己的一套UI,我看有很多開發者將其應用在了一些前端框架中,例如react、vue 。最近自己在學習Angularjs,所以,也想把這個UI整合到這個框架,這幾天試了一下,簡單的套用了一個功能,現在分享給大家,分離做的不好,請高手指點。
適合讀者
有一定的Angularjs基礎,並且了解ngRoute、ngAnimate的人群。
包含文件
整合的時候,參考官方的演示頁面,自己也做了一個演示頁面,完全使用Angularjs做的,沒有引用其它框架。下面先說明一下引進的文件。
使用angular.min.js 1.4.9
使用angular-route.min.js 1.4.9
一開始想與官方的演示頁面一樣做一個單頁面的,開發之後發現,把所有內容放到一個文件裡顯得雜亂,所以,使用了Angularjs的路由功能,把各個小功能獨立成頁面,在需要時加載進來。此處使用模板載入功能來實作。於是,導覽頁面程式碼就顯示很乾淨,如果想要使用哪部分的功能程式碼,直接使用相對應的html頁面及js腳本檔案即可,方便、快速。
下方是導覽頁面的程式碼:
<!DOCTYPE html> <html lang="en" ng-app="weuiapp"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0"> <title>WeUI</title> <link rel="stylesheet" href="./css/weui.css" /> </head> <style type="text/css"> .home, .view { position: absolute; width: 100%; left: 0; top: 0; } </style> <body ng-controller="home"> <p class="home" ng-if="homeShow"> <p class="weui_grids"> <a href="#/button" class="weui_grid js_grid" data-id="button" ng-click="showBlock('button')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_button.png" alt=""> </p> <p class="weui_grid_label"> Button </p> </a> <a href="#/cell" class="weui_grid js_grid" data-id="cell" ng-click="showBlock('cell')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_cell.png" alt=""> </p> <p class="weui_grid_label"> Cell </p> </a> <a href="#/toast" class="weui_grid js_grid" data-id="toast" ng-click="showBlock('toast')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_toast.png" alt=""> </p> <p class="weui_grid_label"> Toast </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="dialog" ng-click="showBlock('dialog')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_dialog.png" alt=""> </p> <p class="weui_grid_label"> Dialog </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="progress" ng-click="showBlock('progress')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_progress.png" alt=""> </p> <p class="weui_grid_label"> Progress </p> </a> <a href="#/msg" class="weui_grid js_grid" data-id="msg" ng-click="showBlock('msg')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_msg.png" alt=""> </p> <p class="weui_grid_label"> Msg </p> </a> <a href="#/article" class="weui_grid js_grid" data-id="article" ng-click="showBlock('article')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_article.png" alt=""> </p> <p class="weui_grid_label"> Article </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="actionsheet" ng-click="showBlock('actionsheet')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_actionSheet.png" alt=""> </p> <p class="weui_grid_label"> ActionSheet </p> </a> <a href="#/icons" class="weui_grid js_grid" data-id="icons" ng-click="showBlock('icons')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_icons.png" alt=""> </p> <p class="weui_grid_label"> Icons </p> </a> <a href="#/panel" class="weui_grid js_grid" data-id="panel" ng-click="showBlock('panel')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_panel.png" alt=""> </p> <p class="weui_grid_label"> Panel </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="tab" ng-click="showBlock('tab')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_tab.png" alt=""> </p> <p class="weui_grid_label"> Tab </p> </a> <a href="#/searchbar" class="weui_grid js_grid" data-id="searchbar" ng-click="showBlock('searchbar')"> <p class="weui_grid_icon"> <img src="./images/icon_nav_search_bar.png" alt=""> </p> <p class="weui_grid_label"> SearchBar </p> </a> </p> </p> <p class="view" ng-view ng-if="viewShow"></p> <script type="text/javascript" src="./js/angular.min.js"></script> <script type="text/javascript" src="./js/angular-animate.min.js"></script> <script type="text/javascript" src="./js/angular-route.min.js"></script> <script type="text/javascript" src="./js/toast.js"></script> <script type="text/javascript" src="./js/example.js"></script> </body> </html>
程式碼中,ngClick中使用了showBlock函數,參數是目前點擊的功能字串,這個函數的參數在使用路由功能後,沒有使用,僅僅是在此函數中增加了隱藏與顯示導航部分以及功能演示部分的程式碼,詳情請查看下面的腳本程式碼。
angular.module('weuiapp', ['ngAnimate', 'ngRoute']) .config(function($routeProvider) { $routeProvider .when('/', { controller: 'home', templateUrl: '' }) .when('/button',{ controller: 'button', templateUrl: 'button.html' }) .when('/cell', { controller: 'cell', templateUrl: 'cell.html' }) .when('/toast', { controller: 'toast', templateUrl: 'toast.html' }) .when('/msg', { controller: 'msg', templateUrl: 'msg.html' }) .when('/article', { controller: 'article', templateUrl: 'article.html' }) .when('/icons', { controller: 'icons', templateUrl: 'icons.html' }) .when('/panel', { controller: 'panel', templateUrl: 'panel.html' }) .otherwise({ redirectTo: '/' }) }) .controller('home', function($scope) { $scope.homeShow = true; $scope.viewShow = false; $scope.showBlock = function() { $scope.homeShow = false; $scope.viewShow = true; } }) .controller('toast', ['$scope', '$interval', toast]) .animation('.aweui-show', ['$animateCss', toastAnimate]) .animation('.home', ['$animateCss', function($animateCss) { return { enter: function(element, doneFn) { return $animateCss(element, { from: { left: '100%', top: 0, opacity: 0 }, to: { left: 0, top: 0, opacity: 1 }, duration: .3 }); }, leave: function(element, doneFn) { return $animateCss(element, { from: { left: 0, top: 0, opacity: 1 }, to: { left: '-100%', top: 0, opacity: 0 }, duration: .3 }); } } }]) .animation('.view', ['$animateCss', function($animateCss) { return { enter: function(element, doneFn) { return $animateCss(element, { from: { left: '100%', top: 0, opacity: 0 }, to: { left: 0, top: 0, opacity: 1 }, duration: .3 }); }, leave: function(element, doneFn) { return $animateCss(element, { from: { left: 0, top: 0, opacity: 1 }, to: { left: '-100%', top: 0, opacity: 0 }, duration: .3 }); } } }]) $scope.showBlock = function() { $scope.homeShow = false; $scope.viewShow = true; }
這段便是函數要實現的功能代碼,分別控制變數homeShow以及viewShowhow兩部分的顯示與隱藏。
.animation('.home', ['$animateCss', function($animateCss) { return { enter: function(element, doneFn) { return $animateCss(element, { from: { left: '100%', top: 0, opacity: 0 }, to: { left: 0, top: 0, opacity: 1 }, duration: .3 }); }, leave: function(element, doneFn) { return $animateCss(element, { from: { left: 0, top: 0, opacity: 1 }, to: { left: '-100%', top: 0, opacity: 0 }, duration: .3 }); } } }])
以上是導覽部分顯示時的動畫效果代碼。導航部分初始化為絕對定位,讓其在消失前先做一個向左移出顯示區域,並且漸隱的動畫。當查看完功能演示,回到導航時,進行動畫反轉。這裡使用的ngAnimate的$animateCss服務,並且使用了此服務提供的進入事件enter以及移出事件leave。其它的動畫功能與其相同。
$routeProvider .when('/', { controller: 'home', templateUrl: '' }) .when('/button',{ controller: 'button', templateUrl: 'button.html' }) .when('/cell', { controller: 'cell', templateUrl: 'cell.html' }) .when('/toast', { controller: 'toast', templateUrl: 'toast.html' }) .when('/msg', { controller: 'msg', templateUrl: 'msg.html' }) .when('/article', { controller: 'article', templateUrl: 'article.html' }) .when('/icons', { controller: 'icons', templateUrl: 'icons.html' }) .when('/panel', { controller: 'panel', templateUrl: 'panel.html' }) .otherwise({ redirectTo: '/' })
下面,再來看看功能示範部分的頁面程式碼。
<p class="page"> <p class="hd"> <h1 class="page_title">Toast</h1> </p> <p class="bd spacing"> <a href="javascript:;" class="weui_btn weui_btn_primary" ng-click="showToast()">点击弹出Toast</a> <a href="javascript:;" class="weui_btn weui_btn_primary" ng-click="showLoadingToast()">点击弹出Loading Toast</a> </p> <!--BEGIN toast--> <p id="toast" ng-if="toastHide" class="aweui-show"> <p class="weui_mask_transparent"></p> <p class="weui_toast"> <i class="weui_icon_toast"></i> <p class="weui_toast_content">已完成</p> </p> </p> <!--end toast--> <!-- loading toast --> <p id="loadingToast" ng-if="loadingToastHide" class="weui_loading_toast aweui-show"> <p class="weui_mask_transparent"></p> <p class="weui_toast"> <p class="weui_loading"> <p class="weui_loading_leaf weui_loading_leaf_0"></p> <p class="weui_loading_leaf weui_loading_leaf_1"></p> <p class="weui_loading_leaf weui_loading_leaf_2"></p> <p class="weui_loading_leaf weui_loading_leaf_3"></p> <p class="weui_loading_leaf weui_loading_leaf_4"></p> <p class="weui_loading_leaf weui_loading_leaf_5"></p> <p class="weui_loading_leaf weui_loading_leaf_6"></p> <p class="weui_loading_leaf weui_loading_leaf_7"></p> <p class="weui_loading_leaf weui_loading_leaf_8"></p> <p class="weui_loading_leaf weui_loading_leaf_9"></p> <p class="weui_loading_leaf weui_loading_leaf_10"></p> <p class="weui_loading_leaf weui_loading_leaf_11"></p> </p> <p class="weui_toast_content">数据加载中</p> </p> </p> </p>
在導覽頁面的js中,有一個控制器和一個動畫函數呼叫了此功能頁面腳本程式碼中的函數,分別是控制器函數toast()以及動畫函數toastAnimate()。腳本文件的程式碼如下。
//toast控制器 function toast($scope, $interval) { $scope.toastHide = 0; $scope.loadingToastHide = 0; $scope.showToast = function() { $scope.toastHide = 1; $interval(function() { $scope.toastHide = 0; }, 3000, 1); } $scope.showLoadingToast = function() { $scope.loadingToastHide = 1; $interval(function() { $scope.loadingToastHide = 0; }, 3000, 1); } } //显示与隐藏时的动画,使用ngAnimate中的$animateCss服务 function toastAnimate($animateCss) { return { enter: function(element, doneFn) { return $animateCss(element, { from: { opacity: 0 }, to: { opacity: 1 }, duration: .3 }); }, leave: function(element, doneFn) { return $animateCss(element, { from: { opacity: 1 }, to: { opacity: 0 }, duration: .3 }); } } }