u-tabbar.vue 10 KB

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