cm-header-nav.vue 6.9 KB

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