Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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