navBar.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. Component({
  2. options: {
  3. multipleSlots: true,
  4. addGlobalClass: true
  5. },
  6. properties: {
  7. extClass: {
  8. type: String,
  9. value: ''
  10. },
  11. background: {
  12. type: String,
  13. value: 'rgba(255, 255, 255, 1)',
  14. observer: '_showChange'
  15. },
  16. backgroundColorTop: {
  17. type: String,
  18. value: 'rgba(255, 255, 255, 1)',
  19. observer: '_showChangeBackgroundColorTop'
  20. },
  21. color: {
  22. type: String,
  23. value: 'rgba(0, 0, 0, 1)'
  24. },
  25. title: {
  26. type: String,
  27. value: ''
  28. },
  29. searchText: {
  30. type: String,
  31. value: '点我搜索'
  32. },
  33. searchBar: {
  34. type: Boolean,
  35. value: false
  36. },
  37. back: {
  38. type: Boolean,
  39. value: false
  40. },
  41. home: {
  42. type: Boolean,
  43. value: false
  44. },
  45. iconTheme: {
  46. type: String,
  47. value: 'black'
  48. },
  49. /* animated: {
  50. type: Boolean,
  51. value: true
  52. },
  53. show: {
  54. type: Boolean,
  55. value: true,
  56. observer: '_showChange'
  57. }, */
  58. delta: {
  59. type: Number,
  60. value: 1
  61. }
  62. },
  63. created: function() {
  64. this.getSystemInfo();
  65. },
  66. attached: function() {
  67. this.setStyle(); //设置样式
  68. },
  69. data: {},
  70. pageLifetimes: {
  71. show: function() {
  72. if (getApp().globalSystemInfo.ios) {
  73. this.getSystemInfo();
  74. this.setStyle(); //设置样式1
  75. }
  76. },
  77. hide: function() {}
  78. },
  79. methods: {
  80. setStyle: function(life) {
  81. const {
  82. statusBarHeight,
  83. navBarHeight,
  84. capsulePosition,
  85. navBarExtendHeight,
  86. ios,
  87. windowWidth
  88. } = getApp().globalSystemInfo;
  89. const { back, home, title } = this.data;
  90. let rightDistance = windowWidth - capsulePosition.right; //胶囊按钮右侧到屏幕右侧的边距
  91. let leftWidth = windowWidth - capsulePosition.left; //胶囊按钮左侧到屏幕右侧的边距
  92. let navigationbarinnerStyle = [
  93. `color: ${this.data.color}`,
  94. `background: ${this.data.background}`,
  95. `height:${navBarHeight + navBarExtendHeight}px`,
  96. `padding-top:${statusBarHeight}px`,
  97. `padding-right:${leftWidth}px`,
  98. `padding-bottom:${navBarExtendHeight}px`
  99. ].join(';');
  100. let navBarLeft = [];
  101. if ((back && !home) || (!back && home)) {
  102. navBarLeft = [`width:${capsulePosition.width}px`, `height:${capsulePosition.height}px`].join(';');
  103. } else if ((back && home) || title) {
  104. navBarLeft = [
  105. `width:${capsulePosition.width}px`,
  106. `height:${capsulePosition.height}px`,
  107. `margin-left:${rightDistance}px`
  108. ].join(';');
  109. } else {
  110. navBarLeft = [`width:auto`, `margin-left:0px`].join(';');
  111. }
  112. if (life === 'created') {
  113. this.data = {
  114. navigationbarinnerStyle,
  115. navBarLeft,
  116. navBarHeight,
  117. capsulePosition,
  118. navBarExtendHeight,
  119. ios
  120. };
  121. } else {
  122. this.setData({
  123. navigationbarinnerStyle,
  124. navBarLeft,
  125. navBarHeight,
  126. capsulePosition,
  127. navBarExtendHeight,
  128. ios
  129. });
  130. }
  131. },
  132. _showChange: function(value) {
  133. this.setStyle();
  134. },
  135. // 返回事件
  136. back: function() {
  137. this.triggerEvent('back', { delta: this.data.delta });
  138. },
  139. home: function() {
  140. this.triggerEvent('home', {});
  141. },
  142. search: function() {
  143. this.triggerEvent('search', {});
  144. },
  145. getSystemInfo() {
  146. var app = getApp();
  147. if (app.globalSystemInfo && !app.globalSystemInfo.ios) {
  148. return app.globalSystemInfo;
  149. } else {
  150. let systemInfo = wx.getSystemInfoSync();
  151. let ios = !!(systemInfo.system.toLowerCase().search('ios') + 1);
  152. let rect;
  153. try {
  154. rect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : null;
  155. if (rect === null) {
  156. throw 'getMenuButtonBoundingClientRect error';
  157. }
  158. //取值为0的情况 有可能width不为0 top为0的情况
  159. if (!rect.width || !rect.top || !rect.left || !rect.height) {
  160. throw 'getMenuButtonBoundingClientRect error';
  161. }
  162. } catch (error) {
  163. let gap = ''; //胶囊按钮上下间距 使导航内容居中
  164. let width = 96; //胶囊的宽度
  165. if (systemInfo.platform === 'android') {
  166. gap = 8;
  167. width = 96;
  168. } else if (systemInfo.platform === 'devtools') {
  169. if (ios) {
  170. gap = 5.5; //开发工具中ios手机
  171. } else {
  172. gap = 7.5; //开发工具中android和其他手机
  173. }
  174. } else {
  175. gap = 4;
  176. width = 88;
  177. }
  178. if (!systemInfo.statusBarHeight) {
  179. //开启wifi的情况下修复statusBarHeight值获取不到
  180. systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
  181. }
  182. rect = {
  183. //获取不到胶囊信息就自定义重置一个
  184. bottom: systemInfo.statusBarHeight + gap + 32,
  185. height: 32,
  186. left: systemInfo.windowWidth - width - 10,
  187. right: systemInfo.windowWidth - 10,
  188. top: systemInfo.statusBarHeight + gap,
  189. width: width
  190. };
  191. console.log('error', error);
  192. console.log('rect', rect);
  193. }
  194. let navBarHeight = '';
  195. if (!systemInfo.statusBarHeight) {
  196. systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
  197. navBarHeight = (function() {
  198. let gap = rect.top - systemInfo.statusBarHeight;
  199. return 2 * gap + rect.height;
  200. })();
  201. systemInfo.statusBarHeight = 0;
  202. systemInfo.navBarExtendHeight = 0; //下方扩展4像素高度 防止下方边距太小
  203. } else {
  204. navBarHeight = (function() {
  205. let gap = rect.top - systemInfo.statusBarHeight;
  206. return systemInfo.statusBarHeight + 2 * gap + rect.height;
  207. })();
  208. if (ios) {
  209. systemInfo.navBarExtendHeight = 4; //下方扩展4像素高度 防止下方边距太小
  210. } else {
  211. systemInfo.navBarExtendHeight = 0;
  212. }
  213. }
  214. systemInfo.navBarHeight = navBarHeight; //导航栏高度不包括statusBarHeight
  215. systemInfo.capsulePosition = rect; //右上角胶囊按钮信息bottom: 58 height: 32 left: 317 right: 404 top: 26 width: 87 目前发现在大多机型都是固定值 为防止不一样所以会使用动态值来计算nav元素大小
  216. systemInfo.ios = ios; //是否ios
  217. app.globalSystemInfo = systemInfo; //将信息保存到全局变量中,后边再用就不用重新异步获取了
  218. //console.log('systemInfo', systemInfo);
  219. return systemInfo;
  220. }
  221. }
  222. }
  223. });