new-nav.vue 8.1 KB

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