cm-header.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view class="cm-header-nav" :style="[headerNavStyle, { backgroundColor: bgColor }]">
  3. <view class="cm-header" :style="[headerStyle]">
  4. <!-- 左侧按钮组 -->
  5. <view class="header-left" :style="[headerLeftStyle]">
  6. <view
  7. class="button-group"
  8. :class="{ border: border }"
  9. :style="[
  10. buttonCount === 1 ? buttonGroupStyleOne : buttonGroupStyleTwo,
  11. { backgroundColor: groupBgColor, borderColor: borderColor }
  12. ]"
  13. >
  14. <view
  15. class="btn iconfont"
  16. v-if="groupLeftButton"
  17. :class="groupLeftButton.icon"
  18. :style="{ color: groupLeftButton.color }"
  19. @click.stop="handleLeftClick(0)"
  20. ></view>
  21. <view class="btn iconfont icon-vertical_line" v-if="buttonCount > 1"></view>
  22. <view
  23. class="btn iconfont"
  24. v-if="groupRightButton"
  25. :class="groupRightButton.icon"
  26. :style="{ color: groupRightButton.color }"
  27. @click.stop="handleLeftClick(1)"
  28. ></view>
  29. </view>
  30. </view>
  31. <!-- 标题 -->
  32. <view class="header-title" :class="align" :style="{ fontSize: systemInfo.fontSizeSetting + 'px' }">
  33. <text>{{ title | formatTitle }}</text>
  34. <text
  35. class="btn iconfont icon-sousuo"
  36. :class="rightButton.icon"
  37. :style="{ color: rightButton.color }"
  38. v-if="rightButton"
  39. @click.stop="handleRightClick"
  40. ></text>
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. export default {
  47. name: 'cm-header',
  48. props: {
  49. // 导航标题
  50. title: {
  51. type: String,
  52. default: '页面标题页面标题页面标题'
  53. },
  54. align: {
  55. type: String,
  56. default: 'center'
  57. },
  58. // 是否显示按钮组边框
  59. border: {
  60. type: Boolean,
  61. default: true
  62. },
  63. // 按钮组数据
  64. leftButtonGroup: {
  65. type: Array,
  66. default: () => [
  67. {
  68. icon: 'icon-daohangfanhui',
  69. color: '#333'
  70. },
  71. {
  72. icon: 'icon-fanhuishouye',
  73. color: '#333'
  74. }
  75. ]
  76. },
  77. // 标题按钮数据
  78. rightButton: {
  79. type: Object,
  80. default: () => ({
  81. icon: 'icon-sousuo',
  82. color: '#333'
  83. })
  84. },
  85. // 按钮组背景色
  86. groupBgColor: {
  87. type: String,
  88. default: '#fff'
  89. },
  90. // 按钮组边框颜色
  91. borderColor: {
  92. type: String,
  93. default: '#eee'
  94. },
  95. // 导航栏背景色
  96. bgColor: {
  97. type: String,
  98. default: '#fff'
  99. }
  100. },
  101. filters: {
  102. formatTitle(title) {
  103. if (!title) return '页面标题'
  104. return title.substring(0, 6) + (title.length > 6 ? '...' : '')
  105. }
  106. },
  107. data() {
  108. return {
  109. height: 0,
  110. menuButtonInfo: {},
  111. systemInfo: {}
  112. }
  113. },
  114. created() {
  115. this.getSystemInfo()
  116. },
  117. computed: {
  118. groupLeftButton() {
  119. return this.leftButtonGroup[0]
  120. },
  121. groupRightButton() {
  122. return this.leftButtonGroup[1]
  123. },
  124. buttonCount() {
  125. return this.leftButtonGroup.length
  126. },
  127. // 导航栏样式
  128. headerNavStyle() {
  129. const { height, top } = this.menuButtonInfo
  130. this.$emit('init', { height: height + top })
  131. return {
  132. height: height + 'px',
  133. paddingTop: top + 'px',
  134. lineHeight: height + 'px'
  135. }
  136. },
  137. // 导航栏可用
  138. headerStyle() {
  139. const { height, width, right, left } = this.menuButtonInfo
  140. const { screenWidth } = this.systemInfo
  141. return {
  142. width: left + 'px',
  143. height: height + 'px',
  144. padding: `0 ${screenWidth - right}px`
  145. }
  146. },
  147. // 导航左侧样式
  148. headerLeftStyle() {
  149. const { height, width, right } = this.menuButtonInfo
  150. const { screenWidth } = this.systemInfo
  151. return {
  152. width: width + 'px',
  153. height: height + 'px',
  154. marginRight: `${screenWidth - right}px`
  155. }
  156. },
  157. // 一个按钮时的样式
  158. buttonGroupStyleOne() {
  159. const { height, width, right } = this.menuButtonInfo
  160. return {
  161. width: height + 'px',
  162. height: height + 'px',
  163. borderRadius: height / 2 + 'px'
  164. }
  165. },
  166. // 两个按钮时的样式
  167. buttonGroupStyleTwo() {
  168. const { height, width, right } = this.menuButtonInfo
  169. const { screenWidth } = this.systemInfo
  170. return {
  171. width: width + 'px',
  172. height: height + 'px',
  173. borderRadius: height / 2 + 'px',
  174. padding: `0 ${screenWidth - right}px`
  175. }
  176. }
  177. },
  178. methods: {
  179. // 获取系统信息
  180. getSystemInfo() {
  181. this.menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  182. this.systemInfo = uni.getSystemInfoSync()
  183. console.log(this.menuButtonInfo)
  184. },
  185. // 按钮组点击事件
  186. handleLeftClick(index) {
  187. uni.navigateBack({
  188. delta:1
  189. })
  190. this.$emit('leftClick', { index })
  191. },
  192. // 标题按钮点击事件
  193. handleRightClick() {
  194. this.$emit('rightClick')
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. .cm-header-nav {
  201. width: 100%;
  202. position: relative;
  203. z-index: 100000;
  204. background: #fff;
  205. .cm-header {
  206. display: flex;
  207. justify-content: space-between;
  208. align-items: center;
  209. box-sizing: border-box;
  210. }
  211. .button-group {
  212. display: flex;
  213. justify-content: space-between;
  214. align-items: center;
  215. box-sizing: border-box;
  216. background: #eee;
  217. &.border {
  218. border: 1px solid #ccc;
  219. }
  220. .btn {
  221. text-align: center;
  222. flex: 1;
  223. &.icon-vertical_line {
  224. flex: unset;
  225. }
  226. }
  227. }
  228. .header-title {
  229. display: flex;
  230. flex: 1;
  231. justify-content: center;
  232. .btn {
  233. margin-left: 4px;
  234. height: 100%;
  235. }
  236. &.left {
  237. justify-content: space-between;
  238. }
  239. }
  240. }
  241. </style>