mysNavBar.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <template>
  2. <view class="nav">
  3. <view class="nv_padding" v-if="!!mysNavConfig.navPadding" :style="{
  4. height: navBarHeight,
  5. }"></view>
  6. <view class="bg_box" style="background-color: #FFFFFF;" :style="{
  7. backgroundColor: mysNavConfig.bgColor,
  8. backgroundImage: mysNavConfig.bgImage,
  9. paddingTop: navBarTop,
  10. height: navBarHeight,
  11. opacity: opacity,
  12. }" :class="{ fixed: mysNavConfig.navFixed }"></view>
  13. <view class="mys_navBar" :style="{
  14. paddingTop: navBarTop,
  15. height: navBarHeight,
  16. paddingRight: navBarRight,
  17. }" :class="{ fixed: mysNavConfig.navFixed }">
  18. <view class="my_left_box" :style="{ height: btnHeight }">
  19. <!-- 左侧单图标 + text -->
  20. <view class="my_left_icon" @tap="onLeftHome" :style="{ height: btnHeight, paddingLeft: navBarLeft }"
  21. v-if="isHome">
  22. <image class="nv_icon" :src="mysNavConfig.homeIconPath || homeIconPath" />
  23. </view>
  24. <view class="my_left_icon" @tap="onLeftClick" v-if="mysNavConfig.btnType !== 'type2' && !isHome"
  25. :style="{ height: btnHeight, paddingLeft: navBarLeft }">
  26. <image class="nv_icon" :src="mysNavConfig.leftIconPath || backIconPath" />
  27. <view class="leftText" :style="{ height: btnHeight }"
  28. v-if="!!mysNavConfig.leftText && mysNavConfig.btnType !== 'type2'">
  29. <text :style="{
  30. color: mysNavConfig.leftText.color,
  31. fontSize: mysNavConfig.leftText.fontSize,
  32. fontWeight: mysNavConfig.leftText.fontWeight,
  33. }">{{ mysNavConfig.leftText.text }}</text>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 左slot -->
  38. <view class="my_left_custom" :style="{ height: btnHeight }">
  39. <slot name="left"></slot>
  40. </view>
  41. <view class="btn_type" :style="{ height: btnHeight }">
  42. <view class="btn_box nav_btn_t1" :style="{
  43. width: btnWidth,
  44. height: btnHeight,
  45. marginLeft: navBarLeft,
  46. borderRadius: mysNavConfig.type2Config.radius,
  47. }" v-if="mysNavConfig.btnType == 'type2' && !isHome">
  48. <view class="btn_back" @tap="onLeftClick">
  49. <image class="nv_icon" :src="mysNavConfig.type2Config.leftPath"
  50. :style="{ height: btnHeight, width: iconW }" mode="aspectFit" />
  51. </view>
  52. <view class="line"></view>
  53. <view class="btn_home" @tap="onLeftHome">
  54. <image class="nv_icon" :src="mysNavConfig.type2Config.rightPath"
  55. :style="{ height: btnHeight, width: iconW }" mode="aspectFit" />
  56. </view>
  57. </view>
  58. <!-- 中间slot -->
  59. <view class="my_custom_box" :style="{ height: btnHeight }">
  60. <!-- 搜索框 -->
  61. <view class="nv_search_box" v-if="!!mysNavConfig.searchConfig" :style="{
  62. height: inputHeight,
  63. backgroundColor: mysNavConfig.searchConfig.bgColor,
  64. marginRight: mysNavConfig.searchConfig.marginRight,
  65. marginLeft: mysNavConfig.searchConfig.marginLeft,
  66. }">
  67. <image class="nv_icon" :src="`/static/searchB.png`" mode="aspectFit" />
  68. <input type="text" :value="mysNavConfig.searchConfig.value"
  69. :placeholder="mysNavConfig.searchConfig.placeholder"
  70. :disabled="mysNavConfig.searchConfig.disabled" @tap="clickInput" @input="changeInput"
  71. @confirm="submitInput" @confirm-type="mysNavConfig.searchConfig.ctype || 'search'"
  72. :placeholder-style="mysNavConfig.searchConfig.placeholderStyle || ''" class="nv_input" />
  73. <view class="close" @tap="clearInput" :style="{ height: inputHeight }">
  74. <image v-if="mysNavConfig.searchConfig.value !== ''" class="nv_icon"
  75. :src="`/static/colse_fill.png`" mode="aspectFit" />
  76. </view>
  77. </view>
  78. <slot name="center1"></slot>
  79. </view>
  80. </view>
  81. <!-- 标题 -->
  82. <view class="title_container" v-if="!mysNavConfig.searchConfig"
  83. :style="{ top: navBarTop, height: btnHeight }">
  84. <text class="title_text" v-if="!!mysNavConfig.navTitle" :style="{
  85. color: mysNavConfig.navTitle.color,
  86. fontSize: mysNavConfig.navTitle.fontSize,
  87. fontWeight: mysNavConfig.navTitle.fontWeight,
  88. }">{{ mysNavConfig.navTitle.text }}</text>
  89. <slot name="center2"></slot>
  90. </view>
  91. <!-- 右边slot -->
  92. <view class="my_right_icon" :style="{ height: btnHeight }">
  93. <image v-if="mysNavConfig.rightIconPath" @tap="onRightClick" class="nv_icon"
  94. :src="mysNavConfig.rightIconPath" />
  95. <slot name="right"></slot>
  96. </view>
  97. </view>
  98. </view>
  99. </template>
  100. <script>
  101. /* 默认首页路径 */
  102. const homePath = "/pages/tasksquare/tasksquare";
  103. /* 默认首页图标路径 */
  104. const homeIconPath = "";
  105. /* 默认返回图标路径 */
  106. const backIconPath = "/static/img/png2.png";
  107. export default {
  108. props: {
  109. mysNavConfig: {
  110. type: Object,
  111. default () {
  112. return {
  113. /* 固定导航 */
  114. navFixed: true,
  115. /* 开启单页显示首页图标 */
  116. isHome: true,
  117. /* 占位开启 */
  118. navPadding: true,
  119. /* 背景 */
  120. bgColor: "#f8f8f8",
  121. /* 渐变色 */
  122. opacity: 1,
  123. /* 渐变开启 */
  124. isOpacity: false,
  125. /* 渐变延时效果 */
  126. opacityTime: 100,
  127. /* 左侧按钮样式 可选 type1 type2 (type2根据小程序胶囊位置调整) */
  128. btnType: "type1",
  129. onLeftClick: "",
  130. /* type1 左侧文字配置 */
  131. leftText: {
  132. text: "",
  133. color: "",
  134. fontSize: "", // px upx rpx
  135. fontWeight: "500", // blod
  136. },
  137. /* 标题 */
  138. navTitle: {
  139. text: "",
  140. color: "",
  141. fontSize: "32rpx", // px upx rpx
  142. fontWeight: "500", // blod
  143. },
  144. /* type2 按钮 */
  145. type2Config: {
  146. // 左图标
  147. leftPath: "/static/back_w.png",
  148. // 右图标
  149. rightPath: "/static/home.png",
  150. // 圆角
  151. radius: "40rpx",
  152. },
  153. /* 搜索框配置 */
  154. // searchConfig: {
  155. // value: "",
  156. // confirmType: "search",
  157. // placeholder: "",
  158. // disabled: true,
  159. // placeholderStyle: "",
  160. // // marginRight: "",
  161. // // marginLeft: "",
  162. // },
  163. /* 自定义事件开启 */
  164. // isCustomFn: false,
  165. /* 定义左侧图标 (大小是固定的16px ,想改变大小通过 slot 或 css ) */
  166. // leftIconPath: "/static/back_b.png",
  167. /* 定义右侧图标 */
  168. // rightIconPath: "/static/home.png",
  169. /* home图标 */
  170. // homeIconPath: "/static/home.png",
  171. };
  172. },
  173. },
  174. scrollTop: {
  175. type: Number,
  176. default: function() {
  177. return 0;
  178. },
  179. },
  180. },
  181. data() {
  182. return {
  183. iconW: undefined,
  184. iconH: undefined,
  185. btnWidth: undefined,
  186. btnHeight: undefined,
  187. navBarTop: undefined,
  188. navBarHeight: undefined,
  189. navBarLeft: undefined,
  190. navBarRight: undefined,
  191. inputHeight: undefined,
  192. opacity: 0,
  193. isHome: false,
  194. homeIconPath: homeIconPath,
  195. backIconPath: backIconPath,
  196. };
  197. },
  198. computed: {
  199. isSharePage() {},
  200. },
  201. watch: {
  202. scrollTop(val) {
  203. if (!this.mysNavConfig.isOpacity) {
  204. return;
  205. }
  206. this.setOpacity(val, this.mysNavConfig.opacityTime);
  207. },
  208. },
  209. methods: {
  210. init() {
  211. if (this.mysNavConfig.isHome) {
  212. let pages = getCurrentPages().length;
  213. /* 单页显示home图标 */
  214. pages == 1 ? (this.isHome = true) : (this.isHome = false);
  215. }
  216. // 开启渐变 强制fixed
  217. this.navBarTop = uni.getSystemInfoSync().statusBarHeight + "px";
  218. if (!this.mysNavConfig.opacity) {
  219. this.opacity = 1;
  220. !this.mysNavConfig.isOpacity ? (this.opacity = 1) : ((this.mysNavConfig.navFixed = true), (this
  221. .opacity = 0));
  222. } else {
  223. this.opacity = this.mysNavConfig.opacity;
  224. !this.mysNavConfig.isOpacity ? "" : ((this.mysNavConfig.navFixed = true), (this.opacity = 0));
  225. }
  226. // #ifdef MP
  227. this.initMP();
  228. // #endif
  229. // #ifdef APP-PLUS-NVUE
  230. this.navBarHeight = 44 + uni.getSystemInfoSync().statusBarHeight + "px";
  231. // #endif
  232. },
  233. /* 小程序初始化位置 */
  234. initMP() {
  235. let gS = uni.getSystemInfoSync();
  236. let rect = uni.getMenuButtonBoundingClientRect();
  237. let rectHeight = rect.height;
  238. this.btnWidth = rect.width + "px";
  239. this.iconW = Math.floor(rect.width / 4 - 5) + "px";
  240. this.iconH = rectHeight / 2 + "px";
  241. this.btnHeight = rectHeight + "px";
  242. this.inputHeight = rectHeight - 3 + "px";
  243. this.navBarTop = rect.top + "px";
  244. this.navBarLeft = gS.screenWidth - rect.right + "px";
  245. this.navBarRight = gS.screenWidth - rect.right + rect.width + "px";
  246. this.navBarHeight = gS.statusBarHeight + rectHeight + (rect.top - gS.statusBarHeight) * 2 + "px";
  247. },
  248. /* 点击左侧 */
  249. onLeftClick() {
  250. // if (this.mysNavConfig.isCustomFn) {
  251. // this.$emit("onLeftClick");
  252. // }
  253. if (this.mysNavConfig.onLeftClick != "") {
  254. console.log(this.mysNavConfig.onLeftClick)
  255. uni.switchTab({
  256. url: this.mysNavConfig.onLeftClick
  257. });
  258. } else {
  259. console.log("back");
  260. let pages = getCurrentPages(); // 当前页面
  261. let beforePage = pages[pages.length - 2]; // 上一页
  262. uni.navigateBack({
  263. success: function() {
  264. beforePage
  265. .onLoad(); // 执行上一页的onLoad方法
  266. }
  267. })
  268. // uni.navigateBack();
  269. }
  270. },
  271. /* type2 返回首页 */
  272. onLeftHome() {
  273. uni.switchTab({
  274. url: homePath,
  275. });
  276. },
  277. /* 点击右侧 */
  278. onRightClick() {
  279. console.log("onRightClick");
  280. uni.showToast({
  281. title: "onRightClick",
  282. duration: 1000,
  283. });
  284. this.$emit("onRightClick");
  285. },
  286. /**
  287. * @param scrollTop 页面顶部距离
  288. * @param down 滑动显示效果延时
  289. */
  290. setOpacity(scrollTop, down = 60) {
  291. if (scrollTop > 0) {
  292. if (scrollTop < down) {
  293. this.opacity = scrollTop / down;
  294. }
  295. if (scrollTop > down) {
  296. this.opacity = 1;
  297. }
  298. } else {
  299. this.opacity = 0;
  300. }
  301. },
  302. /* 搜索框改变值 */
  303. changeInput(e) {
  304. this.$emit("changeInput", e);
  305. },
  306. /* 搜索框提交 */
  307. submitInput(e) {
  308. this.$emit("submitInput", e);
  309. },
  310. /* 清空搜索 */
  311. clearInput() {
  312. this.$emit("clearInput");
  313. },
  314. /* 禁用状态下点击搜索框 */
  315. clickInput() {
  316. this.$emit("clickInput");
  317. },
  318. },
  319. created() {
  320. this.init();
  321. },
  322. onReady() {
  323. // setTimeout(() => {
  324. // // 改变手机状态栏颜色
  325. // uni.setNavigationBarColor({
  326. // frontColor: "#000000",
  327. // backgroundColor: "#FF4258",
  328. // });
  329. // }, 10);
  330. },
  331. };
  332. </script>
  333. <style lang="scss" scoped>
  334. .nav {
  335. position: relative;
  336. }
  337. .nv_padding {
  338. height: 88upx;
  339. background-color: transparent;
  340. }
  341. /* 图标默认大小 */
  342. .nv_icon {
  343. width: 16px;
  344. height: 16px;
  345. }
  346. .bg_box {
  347. position: absolute;
  348. width: 750upx;
  349. height: 88upx;
  350. z-index: 1010;
  351. top: 0;
  352. background-color: transparent;
  353. /* #ifdef MP */
  354. box-sizing: border-box;
  355. /* #endif */
  356. /* #ifndef APP-PLUS-NVUE */
  357. background-size: cover;
  358. background-repeat: no-repeat;
  359. background-position: center center;
  360. /* #endif */
  361. }
  362. .mys_navBar {
  363. /* #ifndef APP-PLUS-NVUE */
  364. display: flex;
  365. height: 88upx;
  366. /* #endif */
  367. // #ifdef MP
  368. box-sizing: border-box;
  369. // #endif
  370. width: 750upx;
  371. flex-direction: row;
  372. background-color: transparent;
  373. position: absolute;
  374. z-index: 1010;
  375. top: 0;
  376. }
  377. .fixed {
  378. position: fixed;
  379. }
  380. /*
  381. left 按钮
  382. */
  383. .btn_type {
  384. /* #ifndef APP-PLUS-NVUE */
  385. display: flex;
  386. /* #endif */
  387. /* #ifdef APP-PLUS-NVUE */
  388. margin-left: 16upx;
  389. /* #endif */
  390. // #ifndef MP
  391. align-items: center;
  392. // #endif
  393. flex: 1;
  394. // background-color: #fff;
  395. flex-direction: row;
  396. }
  397. .btn_box {
  398. /* #ifndef APP-PLUS-NVUE */
  399. display: flex;
  400. margin-left: 16upx;
  401. /* #endif */
  402. flex-direction: row;
  403. // #ifndef MP
  404. align-items: center;
  405. // #endif
  406. }
  407. .btn_back,
  408. .btn_home {
  409. /* #ifndef APP-PLUS-NVUE */
  410. display: flex;
  411. /* #endif */
  412. /* #ifndef APP-PLUS-NVUE */
  413. height: 100%;
  414. /* #endif */
  415. /* #ifdef APP-PLUS-NVUE */
  416. width: 76upx;
  417. height: 62upx;
  418. /* #endif */
  419. flex-direction: row;
  420. align-items: center;
  421. justify-content: center;
  422. flex: 1;
  423. }
  424. /* 按钮类型1 */
  425. .nav_btn_t1 {
  426. /* #ifndef APP-PLUS-NVUE */
  427. display: flex;
  428. /* #endif */
  429. // #ifdef MP
  430. box-sizing: border-box;
  431. // #endif
  432. flex-direction: row;
  433. align-items: center;
  434. border-style: solid;
  435. border-width: 1px;
  436. border-color: #C0C0C0;
  437. // background-color: rgba(0, 0, 0, 0.2);
  438. background-color: #FFFFFF;
  439. width: 150upx;
  440. height: 45upx;
  441. }
  442. .line {
  443. width: 1upx;
  444. height: 60%;
  445. /* #ifdef APP-PLUS-NVUE */
  446. height: 30upx;
  447. /* #endif */
  448. background-color: #C0C0C0;
  449. }
  450. /*
  451. 标题
  452. */
  453. .title_container {
  454. /* #ifndef APP-PLUS-NVUE */
  455. display: flex;
  456. /* #endif */
  457. position: absolute;
  458. left: 375upx;
  459. bottom: 0;
  460. transform: translateX(-50%);
  461. flex-direction: row;
  462. justify-content: center;
  463. align-items: center;
  464. }
  465. .title_text {
  466. // flex: 1;
  467. text-overflow: ellipsis;
  468. /* #ifndef APP-PLUS-NVUE */
  469. white-space: nowrap;
  470. overflow: hidden;
  471. font-weight: 550;
  472. /* #endif */
  473. /* #ifdef APP-PLUS-NVUE */
  474. lines: 1;
  475. /* #endif */
  476. }
  477. /*
  478. 左边自定义
  479. */
  480. .my_left_box {
  481. /* #ifndef APP-PLUS-NVUE */
  482. display: flex;
  483. /* #endif */
  484. flex-direction: row;
  485. align-items: center;
  486. }
  487. .my_left_icon {
  488. /* #ifndef APP-PLUS-NVUE */
  489. display: flex;
  490. /* #endif */
  491. flex-direction: row;
  492. align-items: center;
  493. justify-content: center;
  494. /* #ifndef MP */
  495. margin-left: 18upx;
  496. /* #endif */
  497. }
  498. .my_left_custom {
  499. /* #ifndef APP-PLUS-NVUE */
  500. display: flex;
  501. /* #endif */
  502. flex-direction: row;
  503. align-items: center;
  504. // justify-content: center;
  505. }
  506. /* 文字 */
  507. .leftText {
  508. /* #ifndef APP-PLUS-NVUE */
  509. display: flex;
  510. /* #endif */
  511. flex-direction: row;
  512. align-items: center;
  513. justify-content: center;
  514. padding-left: 4upx;
  515. font-size: 15px;
  516. }
  517. /*
  518. 中间
  519. */
  520. .my_custom_box {
  521. /* #ifndef APP-PLUS-NVUE */
  522. display: flex;
  523. /* #endif */
  524. flex-direction: row;
  525. align-items: center;
  526. justify-content: center;
  527. flex: 1;
  528. }
  529. /*
  530. 搜索框
  531. */
  532. .nv_search_box {
  533. /* #ifndef APP-PLUS-NVUE */
  534. display: flex;
  535. /* #endif */
  536. flex-direction: row;
  537. align-items: center;
  538. flex: 1;
  539. /* #ifndef MP */
  540. height: 60upx;
  541. /* #endif */
  542. background-color: #f8f8f8;
  543. padding-right: 20upx;
  544. padding-left: 20upx;
  545. border-radius: 60upx;
  546. margin-right: 15upx;
  547. margin-left: 15upx;
  548. }
  549. .nv_input {
  550. flex: 1;
  551. padding-left: 16upx;
  552. font-size: 13px;
  553. }
  554. .close {
  555. width: 50upx;
  556. /* #ifndef APP-PLUS-NVUE */
  557. display: flex;
  558. /* #endif */
  559. flex-direction: row;
  560. align-items: center;
  561. justify-content: center;
  562. /* #ifndef MP */
  563. height: 62upx;
  564. /* #endif */
  565. }
  566. /*
  567. 右边 自定义
  568. */
  569. .my_right_icon {
  570. /* #ifndef APP-PLUS-NVUE */
  571. display: flex;
  572. /* #endif */
  573. flex-direction: row;
  574. align-items: center;
  575. justify-content: center;
  576. /* #ifndef MP */
  577. // min-width: 70upx;
  578. /* #endif */
  579. padding-left: 6px;
  580. padding-right: 10px;
  581. }
  582. </style>