tabbar.vue 10 KB

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