index.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <title>首页</title>
  7. <script src="js/mui.min.js"></script>
  8. <link href="css/mui.min.css" rel="stylesheet" />
  9. <style>
  10. html,
  11. body {
  12. background-color: #efeff4;
  13. }
  14. .title {
  15. margin: 20px 15px 10px;
  16. color: #6d6d72;
  17. font-size: 15px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="mui-content">
  23. <div class="title">
  24. <p>这是使用nativeObj 原生控件绘制的底部选项卡示例,当前为父页面,显示第一个tab项内容;</p>
  25. <p>这里采用将第一个tab项内容放在父页面显示,因为是入口页面,会在启动中进行渲染,使首页显示速度更快</p>
  26. <p>选项卡常用于App首页,为加快渲染,原生底部选项卡是在manifest.json中通过plus -> launchwebview -> subNViews 节点配置的;</p>
  27. <p>选项卡图标使用字体绘制,点击可切换对应选项卡的高亮状态,开发者可自定义相应的点击事件;</p>
  28. <p>本示例中,点击第二个选项卡打开一个支持下拉刷新的webview;点击第四个选项卡,打开一个新窗口。</p>
  29. <p>中间悬浮大球图标,因涉及屏幕分辨率动态计算,目前是在首页plusReady事件中实现绘制的。该悬浮大球支持点击事件,开发者可定制实现对应的点击逻辑。</P>
  30. <p>为提高性能,本示例选项卡图标全部使用字体文件绘制(推荐),实际使用中也可以使用图片绘制。</p>
  31. </div>
  32. </div>
  33. <script src="js/util.js"></script>
  34. <script type="text/javascript">
  35. var aniShow = {};
  36. mui.init({
  37. swipeBack: true //启用右滑关闭功能
  38. });
  39. mui.plusReady(function() {
  40. var self = plus.webview.currentWebview(),
  41. nviews = self.getSubNViews(),
  42. subpages = util.options.subpages,
  43. leftPos = Math.ceil((window.innerWidth - 60) / 2); // 设置凸起大图标为水平居中
  44. /**
  45. * drawNativeIconBg 绘制带边框的半圆,
  46. * 实现原理:
  47. * id为bg的tag 创建带边框的圆
  48. * id为bg2的tag 创建白色矩形遮住圆下半部分,只显示凸起带边框部分
  49. * 注意创建先后顺序,创建越晚的层级越高
  50. */
  51. var drawNativeIconBg = util.drawNative('iconBg', {
  52. bottom: '5px',
  53. left: leftPos + 'px',
  54. width: '60px',
  55. height: '60px',
  56. position: 'dock'
  57. }, [{
  58. tag: 'rect',
  59. id: 'bg',
  60. position: {
  61. top: '0px',
  62. left: '0px',
  63. width: '100%',
  64. height: '100%'
  65. },
  66. rectStyles: {
  67. color: '#fff',
  68. radius: '30px',
  69. borderColor: '#ccc',
  70. borderWidth: '1px'
  71. }
  72. }, {
  73. tag: 'rect',
  74. id: 'bg2',
  75. position: {
  76. bottom: '0px',
  77. left: '0px',
  78. width: '100%',
  79. height: '45px'
  80. },
  81. rectStyles: {
  82. color: '#fff'
  83. }
  84. }]);
  85. /**
  86. * 凸起图标最后创建 浮在最顶层
  87. * id为iconBg的红色背景图
  88. * id为icon的字体图标
  89. *
  90. */
  91. var drawNativeIcon = util.drawNative('tabIcon', {
  92. bottom: '10px',
  93. left: leftPos + 5 + 'px',
  94. width: '50px',
  95. height: '50px',
  96. position: 'dock' //此种停靠方式表明该控件应浮在窗口最上层,以免被其他窗口遮住
  97. }, [{
  98. tag: 'rect',
  99. id: 'iconBg',
  100. position: {
  101. top: '0px',
  102. left: '0px',
  103. width: '50px',
  104. height: '100%'
  105. },
  106. rectStyles: {
  107. color: '#d74b28',
  108. size: '50px',
  109. radius: '50%'
  110. }
  111. }, {
  112. tag: 'font',
  113. id: 'icon',
  114. text: '\ue600', //此为字体图标Unicode码'\e600'转换为'\ue600'
  115. position: {
  116. top: '0px',
  117. left: '0px',
  118. width: '50px',
  119. height: '100%'
  120. },
  121. textStyles: {
  122. fontSrc: '_www/fonts/iconfont.ttf',
  123. align: 'center',
  124. color: '#fff',
  125. size: '30px'
  126. }
  127. }]);
  128. // append 到父webview中
  129. self.append(drawNativeIconBg);
  130. self.append(drawNativeIcon);
  131. //自定义监听图标点击事件
  132. drawNativeIcon.addEventListener('click', function(e) {
  133. mui.alert('你点击了图标,你在此可以打开摄像头或者新窗口等自定义点击事件。', '悬浮球点击事件');
  134. // 重绘字体颜色
  135. drawNativeIcon.drawText('\ue600', {}, {
  136. fontSrc: '_www/fonts/iconfont.ttf',
  137. align: 'center',
  138. color: '#000',
  139. size: '30px'
  140. }, 'icon');
  141. });
  142. //创建子webview窗口 并初始化
  143. util.initSubpage();
  144. var activePage = plus.webview.currentWebview();
  145. //给每个view 添加监听点击切换
  146. for(var i = 0; i < 4; i++) {
  147. nviews[i].addEventListener('click', clickEvent, false);
  148. }
  149. // 自定义 tab 点击事件
  150. function clickEvent(e) {
  151. var currId = e.target.id,
  152. currIndex = parseInt(currId.substr(currId.length - 1, 1) - 1),
  153. currView = self.getStyle().subNViews[currIndex];
  154. // 匹配对应tab窗口
  155. if(currIndex > 0){
  156. targetPage = plus.webview.getWebviewById(subpages[currIndex-1]);
  157. }else {
  158. targetPage = plus.webview.currentWebview();
  159. }
  160. if(targetPage == activePage) {
  161. return;
  162. }
  163. if(currIndex !== 3) {
  164. //底部选项卡切换
  165. util.toggleNview(currView, currIndex);
  166. // 子页面切换
  167. util.changeSubpage(targetPage, activePage);
  168. //更改当前活跃的页面
  169. activePage = targetPage;
  170. } else {
  171. //第四个tab 打开新窗口
  172. var newWV = plus.webview.create('html/new-webview.html', 'new');
  173. newWV.show('slide-in-right', 200);
  174. }
  175. }
  176. });
  177. </script>
  178. </body>
  179. </html>