new-nav.vue 8.3 KB

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