as-nav-bar.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <template>
  2. <view class="as-navbar" :class="{'as-dark':dark, 'as-nvue-fixed': fixed}">
  3. <view class="as-navbar__content" :class="{ 'as-navbar--fixed': fixed, 'as-navbar--shadow': shadow, 'as-navbar--border': border }"
  4. :style="{ 'background-image': themeBackground,'background-color': themeBgColor, 'border-bottom-color':themeColor}" >
  5. <status-bar v-if="statusBar" />
  6. <view id="as-navbar__container">
  7. <view :style="{ color: themeColor, height:navbarHeight, padding: padding}"
  8. class="as-navbar__header" id="as-navbar__header">
  9. <view @tap="onClickLeft" class="as-navbar__header-btns as-navbar__header-btns-left"
  10. :style="{width:leftIconWidth}">
  11. <slot name="left">
  12. <view class="as-navbar__content_view" v-if="leftIcon.length > 0">
  13. <uni-icons :color="themeColor" :type="leftIcon" size="20" />
  14. </view>
  15. <view :class="{ 'as-navbar-btn-icon-left': !leftIcon.length > 0 }" class="as-navbar-btn-text"
  16. v-if="leftText.length">
  17. <text :style="{ color: themeColor, fontSize: '12px' }">{{ leftText }}</text>
  18. </view>
  19. </slot>
  20. </view>
  21. <view class="as-navbar__header-container " @tap="onClickTitle">
  22. <slot>
  23. <view class="as-navbar__header-container-inner" v-if="title.length>0">
  24. <text class="as-nav-bar-text as-ellipsis-1 as-nav-bar-title"
  25. :style="{color: themeColor, 'font-size': titleSize }">{{ title }}</text>
  26. </view>
  27. <view class="as-navbar__header-container-inner " v-if="subTitle.length>0">
  28. <text class="as-nav-bar-text as-ellipsis-1 as-nav-bar-subtitle"
  29. :style="{color: themeColor, 'font-size': subTitleSize }">{{ subTitle }}</text>
  30. </view>
  31. </slot>
  32. </view>
  33. <view @click="onClickRight" class="as-navbar__header-btns as-navbar__header-btns-right"
  34. :style="{width:rightIconWidth}">
  35. <slot name="right">
  36. <view v-if="rightIcon.length">
  37. <as-icons :color="themeColor" :type="rightIcon" size="22" />
  38. </view>
  39. <view class="as-navbar-btn-text" v-if="rightText.length && !rightIcon.length">
  40. <text class="as-nav-bar-right-text" :style="{ color: themeColor}">{{ rightText }}</text>
  41. </view>
  42. </slot>
  43. </view>
  44. </view>
  45. <slot name="other"></slot>
  46. </view>
  47. </view>
  48. <!-- #ifndef APP-NVUE -->
  49. <view class="as-navbar__placeholder" v-if="fixed">
  50. <status-bar v-if="statusBar" />
  51. <view class="as-navbar__placeholder-view" :style="{ height: navbarHeight}" />
  52. <view class="as-navbar__placeholder-view" :style="{ height: navOtherHeight}" />
  53. </view>
  54. <!-- #endif -->
  55. </view>
  56. </template>
  57. <script>
  58. import statusBar from "./as-status-bar.vue";
  59. const getVal = (val) => typeof val === 'number' ? val + 'px' : val;
  60. /**
  61. *
  62. *
  63. * NavBar 自定义导航栏
  64. * @description 导航栏组件,主要用于头部导航
  65. * @tutorial https://ext.dcloud.net.cn/plugin?id=52
  66. * @property {Boolean} dark 开启黑暗模式
  67. * @property {String} title 标题文字
  68. * @property {String} leftText 左侧按钮文本
  69. * @property {String} rightText 右侧按钮文本
  70. * @property {String} leftIcon 左侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
  71. * @property {String} rightIcon 右侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
  72. * @property {String} color 图标和文字颜色
  73. * @property {String} backgroundColor 导航栏背景颜色
  74. * @property {Boolean} fixed = [true|false] 是否固定顶部
  75. * @property {Boolean} statusBar = [true|false] 是否包含状态栏
  76. * @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
  77. * @property {Boolean} stat 是否开启统计标题上报
  78. * @event {Function} clickLeft 左侧按钮点击时触发
  79. * @event {Function} clickRight 右侧按钮点击时触发
  80. * @event {Function} clickTitle 中间标题点击时触发
  81. */
  82. export default {
  83. name: "AsNavBar",
  84. components: {
  85. statusBar
  86. },
  87. emits: ['clickLeft', 'clickRight', 'clickTitle'],
  88. props: {
  89. dark: {
  90. type: Boolean,
  91. default: false
  92. },
  93. title: {
  94. type: String,
  95. default: ""
  96. },
  97. subTitle: {
  98. type: String,
  99. default: ""
  100. },
  101. titleSize: {
  102. type: String,
  103. default: '34rpx'
  104. },
  105. subTitleSize: {
  106. type: String,
  107. default: '24rpx'
  108. },
  109. padding: {
  110. type: String,
  111. default: ''
  112. },
  113. leftText: {
  114. type: String,
  115. default: ""
  116. },
  117. rightText: {
  118. type: String,
  119. default: ""
  120. },
  121. leftIcon: {
  122. type: String,
  123. default: ""
  124. },
  125. rightIcon: {
  126. type: String,
  127. default: ""
  128. },
  129. fixed: {
  130. type: [Boolean, String],
  131. default: false
  132. },
  133. color: {
  134. type: String,
  135. default: ""
  136. },
  137. backgroundImage: {
  138. type: String,
  139. default: ""
  140. },
  141. backgroundColor: {
  142. type: String,
  143. default: ""
  144. },
  145. statusBar: {
  146. type: [Boolean, String],
  147. default: false
  148. },
  149. shadow: {
  150. type: [Boolean, String],
  151. default: false
  152. },
  153. border: {
  154. type: [Boolean, String],
  155. default: true
  156. },
  157. height: {
  158. type: [Number, String],
  159. default: 44
  160. },
  161. otherHeight: {
  162. type: [Number, String],
  163. default: 0
  164. },
  165. leftWidth: {
  166. type: [Number, String],
  167. default: 60
  168. },
  169. rightWidth: {
  170. type: [Number, String],
  171. default: 60
  172. },
  173. stat: {
  174. type: [Boolean, String],
  175. default: ''
  176. }
  177. },
  178. computed: {
  179. themeBgColor() {
  180. if (this.dark) {
  181. // 默认值
  182. if (this.backgroundColor) {
  183. return this.backgroundColor
  184. } else {
  185. return this.dark ? '#333' : '#FFF'
  186. }
  187. }
  188. return this.backgroundColor || '#FFF'
  189. },
  190. themeBackground(){
  191. return `url('${this.backgroundImage}')`;
  192. },
  193. themeColor() {
  194. if (this.dark) {
  195. // 默认值
  196. if (this.color) {
  197. return this.color
  198. } else {
  199. return this.dark ? '#fff' : '#333'
  200. }
  201. }
  202. return this.color || '#333'
  203. },
  204. navbarHeight() {
  205. return getVal(this.height)
  206. },
  207. navOtherHeight() {
  208. return getVal(this.otherHeight)
  209. },
  210. leftIconWidth() {
  211. return getVal(this.leftWidth)
  212. },
  213. rightIconWidth() {
  214. return getVal(this.rightWidth)
  215. }
  216. },
  217. mounted() {
  218. if (uni.report && this.stat && this.title !== '') {
  219. uni.report('title', this.title)
  220. }
  221. },
  222. methods: {
  223. onClickLeft() {
  224. this.$emit("clickLeft");
  225. },
  226. onClickRight() {
  227. this.$emit("clickRight");
  228. },
  229. onClickTitle() {
  230. this.$emit("clickTitle");
  231. }
  232. }
  233. };
  234. </script>
  235. <style lang="scss" scoped>
  236. $nav-height: 44px;
  237. .as-nvue-fixed {
  238. /* #ifdef APP-NVUE */
  239. position: sticky;
  240. /* #endif */
  241. }
  242. .as-navbar {
  243. // box-sizing: border-box;
  244. }
  245. .as-nav-bar-text {
  246. /* #ifdef APP-PLUS */
  247. font-size: 34rpx;
  248. /* #endif */
  249. /* #ifndef APP-PLUS */
  250. font-size: 14px;
  251. /* #endif */
  252. }
  253. .as-nav-bar-right-text {
  254. font-size: 12px;
  255. }
  256. .as-navbar__content {
  257. position: relative;
  258. // background-color: #fff;
  259. // box-sizing: border-box;
  260. background-color: transparent;
  261. background-size: 100% auto;
  262. }
  263. .as-navbar__content_view {
  264. // box-sizing: border-box;
  265. }
  266. .as-navbar-btn-text {
  267. /* #ifndef APP-NVUE */
  268. display: flex;
  269. /* #endif */
  270. flex-direction: column;
  271. justify-content: flex-start;
  272. align-items: center;
  273. line-height: 12px;
  274. }
  275. .as-navbar__header {
  276. /* #ifndef APP-NVUE */
  277. display: flex;
  278. /* #endif */
  279. padding: 0 10px;
  280. flex-direction: row;
  281. height: $nav-height;
  282. font-size: 12px;
  283. }
  284. .as-navbar__header-btns {
  285. /* #ifndef APP-NVUE */
  286. overflow: hidden;
  287. display: flex;
  288. /* #endif */
  289. flex-wrap: nowrap;
  290. flex-direction: row;
  291. width: 120rpx;
  292. // padding: 0 6px;
  293. justify-content: center;
  294. align-items: center;
  295. /* #ifdef H5 */
  296. cursor: pointer;
  297. /* #endif */
  298. }
  299. .as-navbar__header-btns-left {
  300. /* #ifndef APP-NVUE */
  301. display: flex;
  302. /* #endif */
  303. width: 120rpx;
  304. justify-content: flex-start;
  305. align-items: center;
  306. }
  307. .as-navbar__header-btns-right {
  308. /* #ifndef APP-NVUE */
  309. display: flex;
  310. /* #endif */
  311. flex-direction: row;
  312. // width: 150rpx;
  313. // padding-right: 30rpx;
  314. justify-content: flex-end;
  315. align-items: center;
  316. }
  317. .as-navbar__header-container {
  318. /* #ifndef APP-NVUE */
  319. display: flex;
  320. /* #endif */
  321. flex: 1;
  322. padding: 0 10px;
  323. overflow: hidden;
  324. flex-direction: column;
  325. }
  326. .as-navbar__header-container-inner {
  327. /* #ifndef APP-NVUE */
  328. display: flex;
  329. /* #endif */
  330. flex: 1;
  331. flex-direction: row;
  332. align-items: center;
  333. justify-content: center;
  334. font-size: 12px;
  335. overflow: hidden;
  336. // box-sizing: border-box;
  337. }
  338. .as-navbar__placeholder-view {
  339. height: $nav-height;
  340. }
  341. .as-navbar--fixed {
  342. position: fixed;
  343. z-index: 998;
  344. /* #ifdef H5 */
  345. left: var(--window-left);
  346. right: var(--window-right);
  347. /* #endif */
  348. /* #ifndef H5 */
  349. left: 0;
  350. right: 0;
  351. /* #endif */
  352. }
  353. .as-navbar--shadow {
  354. box-shadow: 0 1px 6px #ccc;
  355. }
  356. .as-navbar--border {
  357. border-bottom-width: 1rpx;
  358. border-bottom-style: solid;
  359. border-bottom-color: #eee;
  360. }
  361. .as-ellipsis-1 {
  362. overflow: hidden;
  363. /* #ifndef APP-NVUE */
  364. white-space: nowrap;
  365. text-overflow: ellipsis;
  366. /* #endif */
  367. /* #ifdef APP-NVUE */
  368. lines: 1;
  369. text-overflow: ellipsis;
  370. /* #endif */
  371. }
  372. // 暗主题配置
  373. .as-dark {}
  374. </style>