首页 > web前端 > js教程 > 正文

Angularjs整合微信UI(weui)

PHP中文网
发布: 2017-03-16 09:35:03
原创
2706 人浏览过

本文给大家推荐的是使用angularjs实现整合微信新推出的UI(weui)的全部过程,有相同需求的小伙伴可以参考下

引子

不久前,微信推出了自己的一套UI,我看有很多开发者将其套用在了一些前端框架中,比如react、vue。最近自己在学习Angularjs,所以,也想把这个UI整合到这个框架,这几天试了一下,简单的套用了一个功能,现在分享给大家,分离做的不好,请高手指点。

适合读者

有一定的Angularjs基础,并且了解ngRoute、ngAnimate的人群。

包含文件

整合的时候,参照官方的演示页面,自己也做了一个演示页面,完全使用Angularjs做的,没有引用其它框架。下面先说明一下引入的文件。

  1. 使用angular.min.js 1.4.9

  2. 使用angular-route.min.js 1.4.9

  3. 使用angular-animate.min.js 1.4.9

  4. 使用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">
  <p class="home" ng-if="homeShow">
    <p class="weui_grids">
      <a href="#/button" class="weui_grid js_grid" data-id="button" ng-click="showBlock(&#39;button&#39;)">
        <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(&#39;cell&#39;)">
        <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(&#39;toast&#39;)">
        <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(&#39;dialog&#39;)">
        <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(&#39;progress&#39;)">
        <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(&#39;msg&#39;)">
        <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(&#39;article&#39;)">
        <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(&#39;actionsheet&#39;)">
        <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(&#39;icons&#39;)">
        <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(&#39;panel&#39;)">
        <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(&#39;tab&#39;)">
        <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(&#39;searchbar&#39;)">
        <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>
登录后复制


以上代码大部分来自官方的首页代码,由于要使用Angularjs,所以增加了相应的属性,包括ngApp,ngController,ngClick,ngIf以及显示功能演示页面的ngView。

代码中,ngClick中使用了showBlock函数,参数是当前点击的功能字符串,这个函数的参数在使用路由功能后,没有使用,仅仅是在此函数中增加了隐藏与显示导航部分以及功能演示部分的代码,详情请查看下面的脚本代码。


angular.module(&#39;weuiapp&#39;, [&#39;ngAnimate&#39;, &#39;ngRoute&#39;])
  .config(function($routeProvider) {
    $routeProvider
      .when(&#39;/&#39;, {
        controller: &#39;home&#39;,
        templateUrl: &#39;&#39;
      })
      .when(&#39;/button&#39;,{
        controller: &#39;button&#39;,
        templateUrl: &#39;button.html&#39;
      })
      .when(&#39;/cell&#39;, {
        controller: &#39;cell&#39;,
        templateUrl: &#39;cell.html&#39;
      })
      .when(&#39;/toast&#39;, {
        controller: &#39;toast&#39;,
        templateUrl: &#39;toast.html&#39;
      })
      .when(&#39;/msg&#39;, {
        controller: &#39;msg&#39;,
        templateUrl: &#39;msg.html&#39;
      })
      .when(&#39;/article&#39;, {
        controller: &#39;article&#39;,
        templateUrl: &#39;article.html&#39;
      })
      .when(&#39;/icons&#39;, {
        controller: &#39;icons&#39;,
        templateUrl: &#39;icons.html&#39;
      })
      .when(&#39;/panel&#39;, {
        controller: &#39;panel&#39;,
        templateUrl: &#39;panel.html&#39;
      })
      .otherwise({
        redirectTo: &#39;/&#39;
      })

  })
  .controller(&#39;home&#39;, function($scope) {
    $scope.homeShow = true;
    $scope.viewShow = false;

    $scope.showBlock = function() {
      $scope.homeShow = false;
      $scope.viewShow = true;
    }
  })
  .controller(&#39;toast&#39;, [&#39;$scope&#39;, &#39;$interval&#39;, toast])
  .animation(&#39;.aweui-show&#39;, [&#39;$animateCss&#39;, toastAnimate])
  .animation(&#39;.home&#39;, [&#39;$animateCss&#39;, function($animateCss) {
    return {
      enter: function(element, doneFn) {
        return $animateCss(element, {
          from: { left: &#39;100%&#39;, 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: &#39;-100%&#39;, top: 0, opacity: 0 },
          duration: .3
        });
      }
    }
  }])
  .animation(&#39;.view&#39;, [&#39;$animateCss&#39;, function($animateCss) {
    return {
      enter: function(element, doneFn) {
        return $animateCss(element, {
          from: { left: &#39;100%&#39;, 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: &#39;-100%&#39;, top: 0, opacity: 0 },
          duration: .3
        });
      }
    }
  }])
$scope.showBlock = function() {
  $scope.homeShow = false;
  $scope.viewShow = true;
}
登录后复制


这一段便是函数要实现的功能代码,分别控制变量homeShow以及viewShow来实现导航与功能演示两部分的显示与隐藏。


.animation(&#39;.home&#39;, [&#39;$animateCss&#39;, function($animateCss) {
  return {
    enter: function(element, doneFn) {
      return $animateCss(element, {
        from: { left: &#39;100%&#39;, 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: &#39;-100%&#39;, top: 0, opacity: 0 },
        duration: .3
      });
    }
  }
}])
登录后复制


以上是导航部分显示时的动画效果代码。导航部分初始化为绝对定位,让其在消失前先做一个向左移出显示区域,并且渐隐的动画。当查看完功能演示,回到导航时,进行动画反转。这里使用的ngAnimate的$animateCss服务,并且使用了此服务提供的进入事件enter以及移出事件leave。其它的动画功能与其相同。


$routeProvider
  .when(&#39;/&#39;, {
    controller: &#39;home&#39;,
    templateUrl: &#39;&#39;
  })
  .when(&#39;/button&#39;,{
    controller: &#39;button&#39;,
    templateUrl: &#39;button.html&#39;
  })
  .when(&#39;/cell&#39;, {
    controller: &#39;cell&#39;,
    templateUrl: &#39;cell.html&#39;
  })
  .when(&#39;/toast&#39;, {
    controller: &#39;toast&#39;,
    templateUrl: &#39;toast.html&#39;
  })
  .when(&#39;/msg&#39;, {
    controller: &#39;msg&#39;,
    templateUrl: &#39;msg.html&#39;
  })
  .when(&#39;/article&#39;, {
    controller: &#39;article&#39;,
    templateUrl: &#39;article.html&#39;
  })
  .when(&#39;/icons&#39;, {
    controller: &#39;icons&#39;,
    templateUrl: &#39;icons.html&#39;
  })
  .when(&#39;/panel&#39;, {
    controller: &#39;panel&#39;,
    templateUrl: &#39;panel.html&#39;
  })
  .otherwise({
    redirectTo: &#39;/&#39;
  })
登录后复制


这是路由服务,对应html中的a标签href属性,所以,此程序中没有使用showBlock函数的参数,已经没有用处了,此函数只是为了增加了动态效果而创造的。

下面,再来看看功能演示部分的页面代码。


<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>
登录后复制


这就是加载等待提示功能的演示页面代码,一共两种样式,其一,显示文字;其二,有一个加载等待的动画并且有提示文字显示。

页面有两个按钮,功能就是分别呼出这两种样式,每种样式呼出后,停留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是静态的,没有动态,所以只需要将官方的演示照搬即可。需要增加动态代码的,现在已只做了这一个,后续还会陆续增加至完成。

相关文章:

通过微信WEUI实现图片上传,后台PHP该如何处理?

WEUI应用之JS常用信息提示弹层的封装

通过微信的WeUI可以学习到哪些知识?

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!