mysNavBar.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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: "", // blod
  136. },
  137. /* 标题 */
  138. navTitle: {
  139. text: "",
  140. color: "",
  141. fontSize: "32rpx", // px upx rpx
  142. fontWeight: "", // 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. uni.navigateBack();
  261. }
  262. },
  263. /* type2 返回首页 */
  264. onLeftHome() {
  265. uni.switchTab({
  266. url: homePath,
  267. });
  268. },
  269. /* 点击右侧 */
  270. onRightClick() {
  271. console.log("onRightClick");
  272. uni.showToast({
  273. title: "onRightClick",
  274. duration: 1000,
  275. });
  276. this.$emit("onRightClick");
  277. },
  278. /**
  279. * @param scrollTop 页面顶部距离
  280. * @param down 滑动显示效果延时
  281. */
  282. setOpacity(scrollTop, down = 60) {
  283. if (scrollTop > 0) {
  284. if (scrollTop < down) {
  285. this.opacity = scrollTop / down;
  286. }
  287. if (scrollTop > down) {
  288. this.opacity = 1;
  289. }
  290. } else {
  291. this.opacity = 0;
  292. }
  293. },
  294. /* 搜索框改变值 */
  295. changeInput(e) {
  296. this.$emit("changeInput", e);
  297. },
  298. /* 搜索框提交 */
  299. submitInput(e) {
  300. this.$emit("submitInput", e);
  301. },
  302. /* 清空搜索 */
  303. clearInput() {
  304. this.$emit("clearInput");
  305. },
  306. /* 禁用状态下点击搜索框 */
  307. clickInput() {
  308. this.$emit("clickInput");
  309. },
  310. },
  311. created() {
  312. this.init();
  313. },
  314. onReady() {
  315. // setTimeout(() => {
  316. // // 改变手机状态栏颜色
  317. // uni.setNavigationBarColor({
  318. // frontColor: "#000000",
  319. // backgroundColor: "#FF4258",
  320. // });
  321. // }, 10);
  322. },
  323. };
  324. </script>
  325. <style lang="scss" scoped>
  326. .nav {
  327. position: relative;
  328. }
  329. .nv_padding {
  330. height: 88upx;
  331. background-color: transparent;
  332. }
  333. /* 图标默认大小 */
  334. .nv_icon {
  335. width: 16px;
  336. height: 16px;
  337. }
  338. .bg_box {
  339. position: absolute;
  340. width: 750upx;
  341. height: 88upx;
  342. z-index: 10;
  343. top: 0;
  344. background-color: transparent;
  345. /* #ifdef MP */
  346. box-sizing: border-box;
  347. /* #endif */
  348. /* #ifndef APP-PLUS-NVUE */
  349. background-size: cover;
  350. background-repeat: no-repeat;
  351. background-position: center center;
  352. /* #endif */
  353. }
  354. .mys_navBar {
  355. /* #ifndef APP-PLUS-NVUE */
  356. display: flex;
  357. height: 88upx;
  358. /* #endif */
  359. // #ifdef MP
  360. box-sizing: border-box;
  361. // #endif
  362. width: 750upx;
  363. flex-direction: row;
  364. background-color: transparent;
  365. position: absolute;
  366. z-index: 10;
  367. top: 0;
  368. }
  369. .fixed {
  370. position: fixed;
  371. }
  372. /*
  373. left 按钮
  374. */
  375. .btn_type {
  376. /* #ifndef APP-PLUS-NVUE */
  377. display: flex;
  378. /* #endif */
  379. /* #ifdef APP-PLUS-NVUE */
  380. margin-left: 16upx;
  381. /* #endif */
  382. // #ifndef MP
  383. align-items: center;
  384. // #endif
  385. flex: 1;
  386. // background-color: #fff;
  387. flex-direction: row;
  388. }
  389. .btn_box {
  390. /* #ifndef APP-PLUS-NVUE */
  391. display: flex;
  392. margin-left: 16upx;
  393. /* #endif */
  394. flex-direction: row;
  395. // #ifndef MP
  396. align-items: center;
  397. // #endif
  398. }
  399. .btn_back,
  400. .btn_home {
  401. /* #ifndef APP-PLUS-NVUE */
  402. display: flex;
  403. /* #endif */
  404. /* #ifndef APP-PLUS-NVUE */
  405. height: 100%;
  406. /* #endif */
  407. /* #ifdef APP-PLUS-NVUE */
  408. width: 76upx;
  409. height: 62upx;
  410. /* #endif */
  411. flex-direction: row;
  412. align-items: center;
  413. justify-content: center;
  414. flex: 1;
  415. }
  416. /* 按钮类型1 */
  417. .nav_btn_t1 {
  418. /* #ifndef APP-PLUS-NVUE */
  419. display: flex;
  420. /* #endif */
  421. // #ifdef MP
  422. box-sizing: border-box;
  423. // #endif
  424. flex-direction: row;
  425. align-items: center;
  426. border-style: solid;
  427. border-width: 1px;
  428. border-color: #C0C0C0;
  429. // background-color: rgba(0, 0, 0, 0.2);
  430. background-color: #FFFFFF;
  431. width: 150upx;
  432. height: 45upx;
  433. }
  434. .line {
  435. width: 1upx;
  436. height: 60%;
  437. /* #ifdef APP-PLUS-NVUE */
  438. height: 30upx;
  439. /* #endif */
  440. background-color: #C0C0C0;
  441. }
  442. /*
  443. 标题
  444. */
  445. .title_container {
  446. /* #ifndef APP-PLUS-NVUE */
  447. display: flex;
  448. /* #endif */
  449. position: absolute;
  450. left: 375upx;
  451. bottom: 0;
  452. transform: translateX(-50%);
  453. flex-direction: row;
  454. justify-content: center;
  455. align-items: center;
  456. }
  457. .title_text {
  458. // flex: 1;
  459. text-overflow: ellipsis;
  460. /* #ifndef APP-PLUS-NVUE */
  461. white-space: nowrap;
  462. overflow: hidden;
  463. font-weight: 550;
  464. /* #endif */
  465. /* #ifdef APP-PLUS-NVUE */
  466. lines: 1;
  467. /* #endif */
  468. }
  469. /*
  470. 左边自定义
  471. */
  472. .my_left_box {
  473. /* #ifndef APP-PLUS-NVUE */
  474. display: flex;
  475. /* #endif */
  476. flex-direction: row;
  477. align-items: center;
  478. }
  479. .my_left_icon {
  480. /* #ifndef APP-PLUS-NVUE */
  481. display: flex;
  482. /* #endif */
  483. flex-direction: row;
  484. align-items: center;
  485. justify-content: center;
  486. /* #ifndef MP */
  487. margin-left: 18upx;
  488. /* #endif */
  489. }
  490. .my_left_custom {
  491. /* #ifndef APP-PLUS-NVUE */
  492. display: flex;
  493. /* #endif */
  494. flex-direction: row;
  495. align-items: center;
  496. // justify-content: center;
  497. }
  498. /* 文字 */
  499. .leftText {
  500. /* #ifndef APP-PLUS-NVUE */
  501. display: flex;
  502. /* #endif */
  503. flex-direction: row;
  504. align-items: center;
  505. justify-content: center;
  506. padding-left: 4upx;
  507. font-size: 15px;
  508. }
  509. /*
  510. 中间
  511. */
  512. .my_custom_box {
  513. /* #ifndef APP-PLUS-NVUE */
  514. display: flex;
  515. /* #endif */
  516. flex-direction: row;
  517. align-items: center;
  518. justify-content: center;
  519. flex: 1;
  520. }
  521. /*
  522. 搜索框
  523. */
  524. .nv_search_box {
  525. /* #ifndef APP-PLUS-NVUE */
  526. display: flex;
  527. /* #endif */
  528. flex-direction: row;
  529. align-items: center;
  530. flex: 1;
  531. /* #ifndef MP */
  532. height: 60upx;
  533. /* #endif */
  534. background-color: #f8f8f8;
  535. padding-right: 20upx;
  536. padding-left: 20upx;
  537. border-radius: 60upx;
  538. margin-right: 15upx;
  539. margin-left: 15upx;
  540. }
  541. .nv_input {
  542. flex: 1;
  543. padding-left: 16upx;
  544. font-size: 13px;
  545. }
  546. .close {
  547. width: 50upx;
  548. /* #ifndef APP-PLUS-NVUE */
  549. display: flex;
  550. /* #endif */
  551. flex-direction: row;
  552. align-items: center;
  553. justify-content: center;
  554. /* #ifndef MP */
  555. height: 62upx;
  556. /* #endif */
  557. }
  558. /*
  559. 右边 自定义
  560. */
  561. .my_right_icon {
  562. /* #ifndef APP-PLUS-NVUE */
  563. display: flex;
  564. /* #endif */
  565. flex-direction: row;
  566. align-items: center;
  567. justify-content: center;
  568. /* #ifndef MP */
  569. // min-width: 70upx;
  570. /* #endif */
  571. padding-left: 6px;
  572. padding-right: 10px;
  573. }
  574. </style>