new-nav.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <view>
  3. <!-- 微信小程序头部导航栏 -->
  4. <!-- #ifdef MP-WEIXIN -->
  5. <view class="wx-head-mod" :style="{height:navHeight+'rpx',backgroundColor:navBackgroundColor}">
  6. <view class="wx-head-mod-nav" :style="{height:navigationBarHeight+'rpx',top:statusBarHeight+'rpx'}">
  7. <view class="wx-head-mod-nav-content"
  8. :style="{height:customHeight+'rpx',justifyContent:textAlign === 'center'?'center':'left'}">
  9. <!-- 文本区 -->
  10. <view v-if="textType" class="wx-head-mod-nav-content-mian"
  11. :style="{width:navTextWidth,lineHeight:customHeight + 'rpx',paddingLeft:textPaddingLeft*scaleFactor+'rpx',fontSize:fontSize*scaleFactor+'rpx',fontWeight:fontWeight,color:titleColor}">
  12. {{textContent}}
  13. </view>
  14. <view v-else :style="{width:navTextWidth}">
  15. <slot name="textContent"></slot>
  16. </view>
  17. <!-- 返回按钮 -->
  18. <view v-if="leftShow" class="wx-head-mod-nav-content-back"
  19. :style="{display:isBackShow?'flex':'none'}" @click="backEvent">
  20. <view class="wx-head-mod-nav-content-back-img"
  21. :style="{width:backImageWidth*scaleFactor+'rpx',height:backImageHeight*scaleFactor+'rpx'}">
  22. <uni-icons type="left" size="25" :color="leftIconColor"></uni-icons>
  23. <!-- <image :src="backImageUrl" mode="" style="width: 100%;height: 100%;"></image> -->
  24. </view>
  25. </view>
  26. <view v-else class="wx-head-mod-nav-content-back-slot" :style="{width:leftWidth}">
  27. <slot name="leftContent"></slot>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <view :style="{height:navHeight+'rpx',backgroundColor:navBackgroundColor}">
  33. </view>
  34. <!-- #endif -->
  35. <!-- 除微信小程序之外的其他设备 -->
  36. <!-- #ifndef MP-WEIXIN -->
  37. <view class="other-head-mod"
  38. :style="{height:navHeightValue*scaleFactor+statusBarHeight+'rpx',backgroundColor:navBackgroundColor}">
  39. <view class="other-head-mod-mian"
  40. :style="{height:navHeightValue*scaleFactor+'rpx',justifyContent:textAlign === 'center'?'center':'left'}">
  41. <!-- 返回按钮 -->
  42. <view class="other-head-mod-mian-back" v-show="isBackShow" @click="backEvent">
  43. <view class="other-head-mod-mian-back-img"
  44. :style="{width:backImageWidth*scaleFactor+'rpx',height:backImageHeight*scaleFactor+'rpx'}">
  45. <image :src="backImageUrl" mode="" style="width: 100%;height: 100%;"></image>
  46. </view>
  47. </view>
  48. <!-- 标题 -->
  49. <view class="other-head-mod-mian-title" :style="{width:windowWidth - 184+'rpx',lineHeight:navHeightValue*scaleFactor+'rpx',
  50. paddingLeft:textPaddingLeft*scaleFactor+'rpx',fontSize:fontSize*scaleFactor+'rpx',
  51. fontWeight:fontWeight,color:titleColor}">
  52. {{textContent}}
  53. </view>
  54. </view>
  55. </view>
  56. <!-- #endif -->
  57. </view>
  58. </template>
  59. <script>
  60. const app = getApp()
  61. import {
  62. systemInfo
  63. } from '@/common/js/system-info.js'
  64. export default {
  65. name: "HeadView",
  66. props: {
  67. // 文本区域位置 left:左 center:中
  68. textAlign: {
  69. type: String,
  70. default: 'center'
  71. },
  72. // 文本区内容显示状态
  73. textType: {
  74. type: Boolean,
  75. default: true
  76. },
  77. // 文本区内容
  78. textContent: {
  79. type: String,
  80. default: '哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈就啊哈哈好借好还'
  81. },
  82. // 文本区离左边的距离
  83. textPaddingLeft: {
  84. type: Number,
  85. default: 16
  86. },
  87. // 是否需要返回按钮
  88. isBackShow: {
  89. type: Boolean,
  90. default: true
  91. },
  92. // 文本区字体大小
  93. fontSize: {
  94. type: Number,
  95. default: 20 //px
  96. },
  97. // 文本区字体粗细
  98. fontWeight: {
  99. type: Number,
  100. default: 700
  101. },
  102. // 文本区返回按钮图片宽
  103. backImageWidth: {
  104. type: Number,
  105. default: 12 //px
  106. },
  107. // 文本区返回按钮图片高
  108. backImageHeight: {
  109. type: Number,
  110. default: 24 //px
  111. },
  112. // 返回按钮是否显示
  113. leftShow: {
  114. type: Boolean,
  115. default: true
  116. },
  117. // 返回按钮图标颜色
  118. leftIconColor: {
  119. type: String,
  120. default: '#fff'
  121. },
  122. // 返回按钮图标路径
  123. backImageUrl: {
  124. type: String,
  125. default: '/static/img/toLeft.png'
  126. },
  127. // 导航栏整体背景颜色
  128. navBackgroundColor: {
  129. type: String,
  130. default: 'transparent'
  131. },
  132. // 标题字体颜色
  133. titleColor: {
  134. type: String,
  135. default: '#ffffff',
  136. },
  137. /******** h5端,app端需要传入自定义导航栏高度 *******/
  138. navHeightValue: {
  139. type: Number,
  140. default: 44 //px
  141. }
  142. },
  143. computed: {
  144. // 文本区宽度
  145. navTextWidth() {
  146. if (this.textAlign === 'center') {
  147. return (this.windowWidth - (this.windowWidth - this.menubarLeft) * 2) + 'rpx'
  148. } else {
  149. return this.menubarLeft + 'rpx'
  150. }
  151. },
  152. // 自定义返回宽度
  153. leftWidth() {
  154. return this.menubarLeft + 'rpx'
  155. },
  156. // 文本区paddingLeft
  157. textPaddingleft() {
  158. if (this.textAlign === 'center') {
  159. return '0'
  160. } else {
  161. return this.textPaddingLeft + 'rpx'
  162. }
  163. }
  164. },
  165. data() {
  166. return {
  167. statusBarHeight: app.globalData.statusBarHeight, //状态栏高度
  168. navHeight: app.globalData.navHeight, //头部导航栏总体高度
  169. navigationBarHeight: app.globalData.navigationBarHeight, //导航栏高度
  170. customHeight: app.globalData.customHeight, //胶囊高度
  171. scaleFactor: app.globalData.scaleFactor, //比例系数
  172. menubarLeft: app.globalData.menubarLeft, //胶囊定位的左边left
  173. windowWidth: app.globalData.windowWidth * app.globalData.scaleFactor
  174. };
  175. },
  176. methods: {
  177. backEvent() {
  178. uni.navigateBack({
  179. delta: 1
  180. })
  181. }
  182. },
  183. created() {
  184. /* 获取设备信息 */
  185. const SystemInfomations = systemInfo()
  186. /* 通用平台 */
  187. this.statusBarHeight = SystemInfomations.statusBarHeight //状态栏高度
  188. this.scaleFactor = SystemInfomations.scaleFactor //比例系数
  189. this.windowWidth = SystemInfomations.windowWidth //当前设备的屏幕宽度
  190. /* 微信小程序平台 */
  191. // #ifdef MP-WEIXIN
  192. this.navHeight = SystemInfomations.navHeight + SystemInfomations.statusBarHeight //头部导航栏总高度
  193. this.$emit('navHeight', this.navHeight)
  194. this.navigationBarHeight = SystemInfomations.navHeight //头部导航栏高度
  195. this.customHeight = SystemInfomations.menuButtonHeight //胶囊高度
  196. this.menubarLeft = SystemInfomations.menuButtonLeft //胶囊左边界距离左上角的距离
  197. // #endif
  198. }
  199. }
  200. </script>
  201. <style>
  202. /* #ifdef MP-WEIXIN */
  203. .wx-head-mod {
  204. box-sizing: border-box;
  205. width: 100%;
  206. position: fixed;
  207. top: 0;
  208. left: 0;
  209. }
  210. .wx-head-mod-nav {
  211. box-sizing: border-box;
  212. width: 100%;
  213. position: absolute;
  214. left: 0;
  215. display: flex;
  216. justify-content: center;
  217. align-items: center;
  218. }
  219. .wx-head-mod-nav-content {
  220. box-sizing: border-box;
  221. width: 100%;
  222. display: flex;
  223. justify-content: left;
  224. align-items: center;
  225. position: relative;
  226. }
  227. /* 文本区 */
  228. .wx-head-mod-nav-content-mian {
  229. box-sizing: border-box;
  230. height: 100%;
  231. white-space: nowrap;
  232. text-overflow: ellipsis;
  233. overflow: hidden;
  234. }
  235. /* 返回按钮 */
  236. .wx-head-mod-nav-content-back {
  237. box-sizing: border-box;
  238. width: 60rpx;
  239. height: 100%;
  240. /* background-color: aqua; */
  241. position: absolute;
  242. top: 0;
  243. left: 32rpx;
  244. display: flex;
  245. align-items: center;
  246. justify-content: left;
  247. }
  248. .wx-head-mod-nav-content-back-img {
  249. box-sizing: border-box;
  250. }
  251. .wx-head-mod-nav-content-back-slot {
  252. height: 100%;
  253. /* background-color: aqua; */
  254. position: absolute;
  255. top: 0;
  256. left: 0;
  257. }
  258. /* #endif */
  259. /* #ifndef MP-WEIXIN */
  260. .other-head-mod {
  261. box-sizing: border-box;
  262. width: 100%;
  263. position: fixed;
  264. top: 0;
  265. left: 0;
  266. }
  267. .other-head-mod-mian {
  268. box-sizing: border-box;
  269. width: 100%;
  270. display: flex;
  271. align-items: center;
  272. justify-content: left;
  273. position: absolute;
  274. left: 0;
  275. bottom: 0;
  276. }
  277. /* 返回按钮 */
  278. .other-head-mod-mian-back {
  279. box-sizing: border-box;
  280. height: 100%;
  281. width: 60rpx;
  282. position: absolute;
  283. left: 32rpx;
  284. top: 0;
  285. display: flex;
  286. align-items: center;
  287. }
  288. /* 标题 */
  289. .other-head-mod-mian-title {
  290. box-sizing: border-box;
  291. height: 100%;
  292. white-space: nowrap;
  293. text-overflow: ellipsis;
  294. overflow: hidden;
  295. }
  296. /* #endif */
  297. </style>