navBar.js 7.8 KB

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