u-tabbar.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <view v-if="show" class="u-tabbar" @touchmove.stop.prevent="() => {}">
  3. <view class="u-tabbar__content safe-area-inset-bottom" :style="{
  4. height: $u.addUnit(height),
  5. backgroundColor: bgColor,
  6. }" :class="{
  7. 'u-border-top': borderTop
  8. }">
  9. <view class="u-tabbar__content__item" v-for="(item, index) in list" :key="index" :class="{
  10. 'u-tabbar__content__circle': midButton &&item.midButton
  11. }" @tap.stop="clickHandler(index)" :style="{
  12. backgroundColor: bgColor
  13. }">
  14. <view :class="[
  15. midButton && item.midButton ? 'u-tabbar__content__circle__button' : 'u-tabbar__content__item__button'
  16. ]">
  17. <u-icon
  18. :size="midButton && item.midButton ? midButtonSize : iconSize"
  19. :name="elIconPath(index)"
  20. img-mode="scaleToFill"
  21. :color="elColor(index)"
  22. :custom-prefix="item.customIcon ? 'custom-icon' : 'uicon'"
  23. ></u-icon>
  24. <u-badge :count="item.count" :is-dot="item.isDot"
  25. v-if="item.count"
  26. :offset="[-2, getOffsetRight(item.count, item.isDot)]"
  27. ></u-badge>
  28. </view>
  29. <view class="u-tabbar__content__item__text" :style="{
  30. color: elColor(index)
  31. }">
  32. <text class="u-line-1">{{item.text}}</text>
  33. </view>
  34. </view>
  35. <view v-if="midButton" class="u-tabbar__content__circle__border" :class="{
  36. 'u-border': borderTop,
  37. }" :style="{
  38. backgroundColor: bgColor,
  39. left: midButtonLeft
  40. }">
  41. </view>
  42. </view>
  43. <!-- 这里加上一个48rpx的高度,是为了增高有凸起按钮时的防塌陷高度(也即按钮凸出来部分的高度) -->
  44. <view class="u-fixed-placeholder safe-area-inset-bottom" :style="{
  45. height: `calc(${$u.addUnit(height)} + ${midButton ? 48 : 0}rpx)`,
  46. }"></view>
  47. </view>
  48. </template>
  49. <script>
  50. export default {
  51. emits: ["update:modelValue", "input", "change"],
  52. props: {
  53. // 通过v-model绑定current值
  54. value: {
  55. type: [String, Number],
  56. default: 0
  57. },
  58. modelValue: {
  59. type: [String, Number],
  60. default: 0
  61. },
  62. // 显示与否
  63. show: {
  64. type: Boolean,
  65. default: true
  66. },
  67. // 整个tabbar的背景颜色
  68. bgColor: {
  69. type: String,
  70. default: '#ffffff'
  71. },
  72. // tabbar的高度,默认50px,单位任意,如果为数值,则为rpx单位
  73. height: {
  74. type: [String, Number],
  75. default: '50px'
  76. },
  77. // 非凸起图标的大小,单位任意,数值默认rpx
  78. iconSize: {
  79. type: [String, Number],
  80. default: 40
  81. },
  82. // 凸起的图标的大小,单位任意,数值默认rpx
  83. midButtonSize: {
  84. type: [String, Number],
  85. default: 90
  86. },
  87. // 激活时的演示,包括字体图标,提示文字等的演示
  88. activeColor: {
  89. type: String,
  90. default: '#303133'
  91. },
  92. // 未激活时的颜色
  93. inactiveColor: {
  94. type: String,
  95. default: '#606266'
  96. },
  97. // 是否显示中部的凸起按钮
  98. midButton: {
  99. type: Boolean,
  100. default: false
  101. },
  102. // 配置参数
  103. list: {
  104. type: Array,
  105. default () {
  106. return []
  107. }
  108. },
  109. // 切换前的回调
  110. beforeSwitch: {
  111. type: Function,
  112. default: null
  113. },
  114. // 是否显示顶部的横线
  115. borderTop: {
  116. type: Boolean,
  117. default: true
  118. },
  119. // 是否隐藏原生tabbar
  120. hideTabBar: {
  121. type: Boolean,
  122. default: true
  123. },
  124. },
  125. data() {
  126. return {
  127. // 由于安卓太菜了,通过css居中凸起按钮的外层元素有误差,故通过js计算将其居中
  128. midButtonLeft: '50%',
  129. pageUrl: '', // 当前页面URL
  130. }
  131. },
  132. created() {
  133. // 是否隐藏原生tabbar
  134. if(this.hideTabBar) uni.hideTabBar();
  135. // 获取引入了u-tabbar页面的路由地址,该地址没有路径前面的"/"
  136. let pages = getCurrentPages();
  137. // 页面栈中的最后一个即为项为当前页面,route属性为页面路径
  138. this.pageUrl = pages[pages.length - 1].route;
  139. },
  140. computed: {
  141. elIconPath() {
  142. return (index) => {
  143. // 历遍u-tabbar的每一项item时,判断是否传入了pagePath参数,如果传入了
  144. // 和data中的pageUrl参数对比,如果相等,即可判断当前的item对应当前的tabbar页面,设置高亮图标
  145. // 采用这个方法,可以无需使用v-model绑定的value值
  146. let pagePath = this.list[index].pagePath;
  147. // 如果定义了pagePath属性,意味着使用系统自带tabbar方案,否则使用一个页面用几个组件模拟tabbar页面的方案
  148. // 这两个方案对处理tabbar item的激活与否方式不一样
  149. if(pagePath) {
  150. if(pagePath == this.pageUrl || pagePath == '/' + this.pageUrl) {
  151. return this.list[index].selectedIconPath;
  152. } else {
  153. return this.list[index].iconPath;
  154. }
  155. } else {
  156. // 普通方案中,索引等于v-model值时,即为激活项
  157. return index == this.getValue() ? this.list[index].selectedIconPath : this.list[index].iconPath
  158. }
  159. }
  160. },
  161. elColor() {
  162. return (index) => {
  163. // 判断方法同理于elIconPath
  164. let pagePath = this.list[index].pagePath;
  165. if(pagePath) {
  166. if(pagePath == this.pageUrl || pagePath == '/' + this.pageUrl) return this.activeColor;
  167. else return this.inactiveColor;
  168. } else {
  169. return index == this.getValue() ? this.activeColor : this.inactiveColor;
  170. }
  171. }
  172. }
  173. },
  174. mounted() {
  175. this.midButton && this.getMidButtonLeft();
  176. },
  177. methods: {
  178. getValue(){
  179. // #ifndef VUE3
  180. return this.value;
  181. // #endif
  182. // #ifdef VUE3
  183. return this.modelValue;
  184. // #endif
  185. },
  186. async clickHandler(index) {
  187. if(this.beforeSwitch && typeof(this.beforeSwitch) === 'function') {
  188. // 执行回调,同时传入索引当作参数
  189. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  190. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  191. let beforeSwitch = this.beforeSwitch.bind(this.$u.$parent.call(this))(index);
  192. // 判断是否返回了promise
  193. if (!!beforeSwitch && typeof beforeSwitch.then === 'function') {
  194. await beforeSwitch.then(res => {
  195. // promise返回成功,
  196. this.switchTab(index);
  197. }).catch(err => {
  198. })
  199. } else if(beforeSwitch === true) {
  200. // 如果返回true
  201. this.switchTab(index);
  202. }
  203. } else {
  204. this.switchTab(index);
  205. }
  206. },
  207. // 切换tab
  208. switchTab(index) {
  209. // 发出事件和修改v-model绑定的值
  210. this.$emit('change', index);
  211. // 如果有配置pagePath属性,使用uni.switchTab进行跳转
  212. if(this.list[index].pagePath) {
  213. uni.switchTab({
  214. url: this.list[index].pagePath
  215. })
  216. } else {
  217. // 如果配置了papgePath属性,将不会双向绑定v-model传入的value值
  218. // 因为这个模式下,不再需要v-model绑定的value值了,而是通过getCurrentPages()适配
  219. this.$emit('input', index);
  220. this.$emit("update:modelValue", index);
  221. }
  222. },
  223. // 计算角标的right值
  224. getOffsetRight(count, isDot) {
  225. // 点类型,count大于9(两位数),分别设置不同的right值,避免位置太挤
  226. if(isDot) {
  227. return -20;
  228. } else if(count > 9) {
  229. return -40;
  230. } else {
  231. return -30;
  232. }
  233. },
  234. // 获取凸起按钮外层元素的left值,让其水平居中
  235. getMidButtonLeft() {
  236. let windowWidth = this.$u.sys().windowWidth;
  237. // 由于安卓中css计算left: 50%的结果不准确,故用js计算
  238. this.midButtonLeft = (windowWidth / 2) + 'px';
  239. }
  240. }
  241. }
  242. </script>
  243. <style scoped lang="scss">
  244. @import "../../libs/css/style.components.scss";
  245. .u-fixed-placeholder {
  246. /* #ifndef APP-NVUE */
  247. box-sizing: content-box;
  248. /* #endif */
  249. }
  250. .u-tabbar {
  251. &__content {
  252. @include vue-flex;
  253. align-items: center;
  254. position: relative;
  255. position: fixed;
  256. bottom: 0;
  257. left: 0;
  258. width: 100%;
  259. z-index: 998;
  260. /* #ifndef APP-NVUE */
  261. box-sizing: content-box;
  262. /* #endif */
  263. &__circle__border {
  264. border-radius: 100%;
  265. width: 110rpx;
  266. height: 110rpx;
  267. top: -48rpx;
  268. position: absolute;
  269. z-index: 4;
  270. background-color: #ffffff;
  271. // 由于安卓的无能,导致只有3个tabbar item时,此css计算方式有误差
  272. // 故使用js计算的形式来定位,此处不注释,是因为js计算有延后,避免出现位置闪动
  273. left: 50%;
  274. transform: translateX(-50%);
  275. &:after {
  276. border-radius: 100px;
  277. }
  278. }
  279. &__item {
  280. flex: 1;
  281. justify-content: center;
  282. height: 100%;
  283. padding: 12rpx 0;
  284. @include vue-flex;
  285. flex-direction: column;
  286. align-items: center;
  287. position: relative;
  288. &__button {
  289. position: absolute;
  290. top: 14rpx;
  291. left: 50%;
  292. transform: translateX(-50%);
  293. }
  294. &__text {
  295. color: $u-content-color;
  296. font-size: 26rpx;
  297. line-height: 28rpx;
  298. position: absolute;
  299. bottom: 14rpx;
  300. left: 50%;
  301. transform: translateX(-50%);
  302. width: 100%;
  303. text-align: center;
  304. }
  305. }
  306. &__circle {
  307. position: relative;
  308. @include vue-flex;
  309. flex-direction: column;
  310. justify-content: space-between;
  311. z-index: 10;
  312. /* #ifndef APP-NVUE */
  313. height: calc(100% - 1px);
  314. /* #endif */
  315. &__button {
  316. width: 90rpx;
  317. height: 90rpx;
  318. border-radius: 100%;
  319. @include vue-flex;
  320. justify-content: center;
  321. align-items: center;
  322. position: absolute;
  323. background-color: #ffffff;
  324. top: -40rpx;
  325. left: 50%;
  326. z-index: 6;
  327. transform: translateX(-50%);
  328. }
  329. }
  330. }
  331. }
  332. </style>