3
0

custom-nav-bar.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <view class="ljs-top" :style="'height: calc('+ statusBarHeight +'px + '+ topHeight+'rpx)'">
  3. <view class="topZw">
  4. <view class="comStatus" :style="'height: '+ statusBarHeight +'px'"></view>
  5. <view class="topMain" :style="'height: '+ topHeight+'rpx'"></view>
  6. </view>
  7. <!-- #ifdef MP-WEIXIN -->
  8. <view class="top_bg" :style="{'opacity':opacity}">
  9. <view class="top_bg" :style="{
  10. 'background': !backgroundImageShow?backgroundColor:('url('+backgroundImage+') no-repeat;'),
  11. backgroundSize: '100% 100%'
  12. }"></view>
  13. </view>
  14. <view class="top">
  15. <!-- #endif -->
  16. <!-- #ifndef MP-WEIXIN -->
  17. <view class="top_bg" :style="{'opacity':opacity}">
  18. <view class="top_bg" :style="topStyle"></view>
  19. </view>
  20. <view class="top">
  21. <!-- #endif -->
  22. <!-- 这里是状态栏 占据高度 -->
  23. <view class="comStatus" :style="'height: '+ statusBarHeight +'px'"></view>
  24. <view class="topMain" :style="'height:'+topHeight+'rpx'">
  25. <!-- 返回 -->
  26. <view @click="goBack" class="back" v-if="myback.show">
  27. <u-icon size="20" name="arrow-left"></u-icon>
  28. </view>
  29. <view @click="goBack" class="backLogo" v-if="myback.logoShow">
  30. <image style="width: 160rpx; height: 66rpx;" mode="scaleToFill" :src="logoImage"></image>
  31. </view>
  32. <slot></slot>
  33. <!-- 标题文字 -->
  34. <text class="title" :style="{'color': titleColor}">{{title}}</text>
  35. </view>
  36. </view>
  37. <!-- <view class="yuanhu"></view> -->
  38. </view>
  39. </template>
  40. <script>
  41. import logoImage from '@/static/images/logo.png'
  42. export default {
  43. data() {
  44. return {
  45. logoImage,
  46. opacity: 0,
  47. scrollTop: 0,
  48. statusBarHeight: 0, // 状态按钮高度
  49. myback: {
  50. show: true,
  51. },
  52. // top样式
  53. topStyle: {}
  54. }
  55. },
  56. props: {
  57. // 标题内容
  58. title: String,
  59. // 标题颜色
  60. titleColor: {
  61. type: String,
  62. default: '#000000'
  63. },
  64. // 返回按钮相关配置
  65. back: {
  66. type: Object,
  67. default: function() {
  68. return {
  69. // 是否显示返回按钮,默认显示
  70. show: true,
  71. logoShow: false,
  72. // 返回按钮的图片地址
  73. // imgUrl: require('../../static/images/ico_back.png')
  74. }
  75. }
  76. },
  77. // 开启背景图片,未开启,使用背景颜色,开启backgroundImage为必填项
  78. backgroundImageShow: {
  79. type: Boolean,
  80. default: false
  81. },
  82. // 背景颜色,支持渐变色,如:linear-gradient(to top right, #CDDC39, #8BC34A, #FFEB3B);
  83. backgroundColor: {
  84. type: String,
  85. default: 'linear-gradient(to top right, #CDDC39, #8BC34A, #FFEB3B)'
  86. },
  87. // 背景图片地址,使用前需配置backgroundImageShow为true。
  88. backgroundImage: String,
  89. // 是否保存顶部栏高度
  90. hideHeight: {
  91. type: Boolean,
  92. default: true
  93. },
  94. // 高度(除状态栏)
  95. topHeight: {
  96. type: Number,
  97. default: 80
  98. },
  99. },
  100. mounted() {
  101. this.init();
  102. },
  103. methods: {
  104. goBack() {
  105. const data = getCurrentPages()
  106. if (data.length > 1) {
  107. uni.navigateBack();
  108. } else {
  109. // *使用此法返回需要遵守现有路由层级
  110. const route = data[0].route.split('/')
  111. uni.switchTab({
  112. url: `/${route[0]}/${route[1]}/index`
  113. });
  114. }
  115. },
  116. init() {
  117. // #ifdef MP-WEIXIN
  118. // #endif
  119. // #ifndef MP-WEIXIN
  120. if (this.backgroundImageShow) {
  121. this.topStyle = {
  122. 'background-image': 'url(' + this.backgroundImage + ')',
  123. 'background-repeat': 'no-repeat',
  124. backgroundSize: '100% 100%'
  125. }
  126. } else {
  127. this.topStyle = {
  128. 'background': this.backgroundColor
  129. }
  130. }
  131. // #endif
  132. this.statusBarHeight = this.getSysInfo().statusBarHeight;
  133. for (let key in this.back) {
  134. this.myback[key] = this.back[key];
  135. }
  136. },
  137. getSysInfo() {
  138. return uni.getSystemInfoSync();
  139. },
  140. // 转接到达顶部距离参数
  141. scrollScroll(event) {
  142. this.scrollTop = event
  143. if (event < 200) {
  144. this.opacity = event / 100 / 2
  145. } else {
  146. this.opacity = 1
  147. }
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss" scoped>
  153. .ljs-top {
  154. width: 100%;
  155. position: relative;
  156. .topZw {
  157. width: 100%;
  158. .comStatus {
  159. width: 100%;
  160. position: relative;
  161. }
  162. .topMain {
  163. width: 100%;
  164. display: flex;
  165. align-items: center;
  166. }
  167. }
  168. .top_bg {
  169. width: 100%;
  170. height: 100%;
  171. position: absolute;
  172. top: 0;
  173. left: 0;
  174. z-index: 0;
  175. }
  176. .top {
  177. width: 100%;
  178. position: fixed;
  179. top: 0;
  180. left: 0;
  181. z-index: 90;
  182. .comStatus {
  183. width: 100%;
  184. position: relative;
  185. }
  186. .topMain {
  187. width: 100%;
  188. position: relative;
  189. display: flex;
  190. align-items: center;
  191. padding: 0 20upx;
  192. box-sizing: border-box;
  193. .back {
  194. width: 60upx;
  195. height: 60upx;
  196. box-sizing: border-box;
  197. padding: 10upx;
  198. position: absolute;
  199. top: 10upx;
  200. left: 20upx;
  201. z-index: 2;
  202. .img {
  203. width: 40upx;
  204. height: 40upx;
  205. display: block;
  206. }
  207. }
  208. .backLogo{
  209. width: fit-content;
  210. }
  211. .title {
  212. flex: 1;
  213. height: 58upx;
  214. line-height: 58upx;
  215. text-align: center;
  216. /* #ifndef APP-PLUS-NVUE || APP-NVUE */
  217. position: absolute;
  218. top: 11upx;
  219. left: 50%;
  220. transform: translate(-50%, 0%);
  221. z-index: 90;
  222. /* #endif */
  223. color: #FFF;
  224. font-size: 32upx;
  225. }
  226. }
  227. }
  228. .yuanhu {
  229. width: 100%;
  230. height: 30upx;
  231. border-radius: 30upx 30upx 0 0;
  232. background-color: #FFF;
  233. opacity: 0.1;
  234. position: absolute;
  235. bottom: 0;
  236. left: 0;
  237. z-index: 90;
  238. }
  239. }
  240. </style>