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
- 使用angular-animate.min.js 1.4.9
- 使用weui.min.css 0.4.0
一开始想与官方的演示页面一样做一个单页面的,开发之后发现,把所有内容放到一个文件里显得杂乱,所以,使用了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"> <div class="home" ng-if="homeshow"> <div class="weui_grids"> <a href="#/button" class="weui_grid js_grid" data-id="button" ng-click="showblock('button')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_button.png" alt=""> </div> <p class="weui_grid_label"> button </p> </a> <a href="#/cell" class="weui_grid js_grid" data-id="cell" ng-click="showblock('cell')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_cell.png" alt=""> </div> <p class="weui_grid_label"> cell </p> </a> <a href="#/toast" class="weui_grid js_grid" data-id="toast" ng-click="showblock('toast')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_toast.png" alt=""> </div> <p class="weui_grid_label"> toast </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="dialog" ng-click="showblock('dialog')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_dialog.png" alt=""> </div> <p class="weui_grid_label"> dialog </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="progress" ng-click="showblock('progress')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_progress.png" alt=""> </div> <p class="weui_grid_label"> progress </p> </a> <a href="#/msg" class="weui_grid js_grid" data-id="msg" ng-click="showblock('msg')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_msg.png" alt=""> </div> <p class="weui_grid_label"> msg </p> </a> <a href="#/article" class="weui_grid js_grid" data-id="article" ng-click="showblock('article')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_article.png" alt=""> </div> <p class="weui_grid_label"> article </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="actionsheet" ng-click="showblock('actionsheet')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_actionsheet.png" alt=""> </div> <p class="weui_grid_label"> actionsheet </p> </a> <a href="#/icons" class="weui_grid js_grid" data-id="icons" ng-click="showblock('icons')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_icons.png" alt=""> </div> <p class="weui_grid_label"> icons </p> </a> <a href="#/panel" class="weui_grid js_grid" data-id="panel" ng-click="showblock('panel')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_panel.png" alt=""> </div> <p class="weui_grid_label"> panel </p> </a> <a href="javascript:;" class="weui_grid js_grid" data-id="tab" ng-click="showblock('tab')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_tab.png" alt=""> </div> <p class="weui_grid_label"> tab </p> </a> <a href="#/searchbar" class="weui_grid js_grid" data-id="searchbar" ng-click="showblock('searchbar')"> <div class="weui_grid_icon"> <img src="./images/icon_nav_search_bar.png" alt=""> </div> <p class="weui_grid_label"> searchbar </p> </a> </div> </div> <div class="view" ng-view ng-if="viewshow"></div> <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>
以上代码大部分来自官方的首页代码,由于要使用angularjs,所以增加了相应的属性,包括ngapp,ngcontroller,ngclick,ngif以及显示功能演示页面的ngview。
代码中,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以及viewshow来实现导航与功能演示两部分的显示与隐藏。
.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: '/' })
这是路由服务,对应html中的a标签href属性,所以,此程序中没有使用showblock函数的参数,已经没有用处了,此函数只是为了增加了动态效果而创造的。
下面,再来看看功能演示部分的页面代码。
<div class="page"> <div class="hd"> <h1 class="page_title">toast</h1> </div> <div 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> </div> <!--begin toast--> <div id="toast" ng-if="toasthide" class="aweui-show"> <div class="weui_mask_transparent"></div> <div class="weui_toast"> <i class="weui_icon_toast"></i> <p class="weui_toast_content">已完成</p> </div> </div> <!--end toast--> <!-- loading toast --> <div id="loadingtoast" ng-if="loadingtoasthide" class="weui_loading_toast aweui-show"> <div class="weui_mask_transparent"></div> <div class="weui_toast"> <div class="weui_loading"> <div class="weui_loading_leaf weui_loading_leaf_0"></div> <div class="weui_loading_leaf weui_loading_leaf_1"></div> <div class="weui_loading_leaf weui_loading_leaf_2"></div> <div class="weui_loading_leaf weui_loading_leaf_3"></div> <div class="weui_loading_leaf weui_loading_leaf_4"></div> <div class="weui_loading_leaf weui_loading_leaf_5"></div> <div class="weui_loading_leaf weui_loading_leaf_6"></div> <div class="weui_loading_leaf weui_loading_leaf_7"></div> <div class="weui_loading_leaf weui_loading_leaf_8"></div> <div class="weui_loading_leaf weui_loading_leaf_9"></div> <div class="weui_loading_leaf weui_loading_leaf_10"></div> <div class="weui_loading_leaf weui_loading_leaf_11"></div> </div> <p class="weui_toast_content">数据加载中</p> </div> </div> </div>
这就是加载等待提示功能的演示页面代码,一共两种样式,其一,显示文字;其二,有一个加载等待的动画并且有提示文字显示。
页面有两个按钮,功能就是分别呼出这两种样式,每种样式呼出后,停留3秒后自动消失。
在导航页面的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 }); } } }
到此,导航和一个功能演示的页面就已经实现了。由于大部分ui是静态的,没有动态,所以只需要将官方的演示照搬即可。需要增加动态代码的,现在已只做了这一个,后续还会陆续增加至完成。
代码开源地址
代码已经上传至github上,感兴趣的朋友可以点击查看,代码同时也上传到个人的服务器上,说明中有链接地址,可以直接点击查看效果。
由于项目刚刚成立,很多东西没有完善,还有很多不如意的地方,请谅解。更希望能得到您的帮助与指正。
上一篇: 家庭吃火锅怎么做
下一篇: 炒重庆火锅底料配方有哪些