tui-bottom-navigation.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <template>
  2. <view @touchmove.stop.prevent="stop">
  3. <view class="tui-bottom-navigation" :class="{ 'tui-navigation-fixed': isFixed, 'tui-remove-splitLine': unlined }">
  4. <view
  5. class="tui-navigation-item"
  6. :class="{ 'tui-item-after_height': splitLineScale, 'tui-last-item': index == itemList.length - 1 }"
  7. :style="{ backgroundColor: isDarkMode ? '#202020' : backgroundColor }"
  8. v-for="(item, index) in itemList"
  9. :key="index"
  10. >
  11. <view class="tui-item-inner" @tap="menuClick(index, item.parameter, item.type)">
  12. <image
  13. :src="getIcon(current,index, item)"
  14. class="tui-navigation-img"
  15. v-if="item.iconPath || (current == index && item.selectedIconPath && item.type == 1)"
  16. ></image>
  17. <text
  18. class="tui-navigation-text"
  19. :style="{
  20. color: isDarkMode ? '#fff' : current == index && item.type == 1 ? selectedColor : item.color || color,
  21. fontWeight: current == index && bold && item.type == 1 ? 'bold' : 'normal',
  22. fontSize: fontSize
  23. }"
  24. >
  25. {{ item.text }}
  26. </text>
  27. </view>
  28. <view
  29. class="tui-navigation-popup"
  30. :class="{ 'tui-navigation-popup_show': showMenuIndex == index }"
  31. :style="{ backgroundColor: isDarkMode ? '#4c4c4c' : subMenuBgColor, left: item.popupLeft || '50%' }"
  32. v-if="item.itemList"
  33. >
  34. <view
  35. class="tui-popup-cell"
  36. :class="{ 'tui-first-cell': subIndex === 0, 'tui-last-cell': subIndex === item.itemList.length - 1 }"
  37. :hover-class="subMenuHover ? (isDarkMode ? 'tui-item-dark_hover' : 'tui-item-hover') : ''"
  38. :hover-stay-time="150"
  39. v-for="(subItem, subIndex) in item.itemList || []"
  40. :key="subIndex"
  41. @tap="subMenuClick(index, item.type, subIndex, subItem.parameter || '')"
  42. >
  43. <text class="tui-ellipsis" :style="{ color: isDarkMode ? '#fff' : subMenuColor, fontSize: subMenufontSize, lineHeight: subMenufontSize }">
  44. {{ subItem.text }}
  45. </text>
  46. </view>
  47. <view class="tui-popup-triangle" :style="{ borderTopColor: isDarkMode ? '#4c4c4c' : subMenuBgColor }"></view>
  48. </view>
  49. </view>
  50. </view>
  51. <view class="tui-navigation-mask" :class="{ 'tui-navigation-mask_show': showMenuIndex != -1 }" @tap="handleClose"></view>
  52. </view>
  53. </template>
  54. <script>
  55. export default {
  56. name: 'tuiBottomNavigation',
  57. emits: ['click'],
  58. props: {
  59. //当前索引
  60. current: {
  61. type: Number,
  62. default: 0
  63. },
  64. /**
  65. * {
  66. text: 'ThorUI',
  67. iconPath: '/static/images/common/icon_menu_gray.png',
  68. selectedIconPath: '/static/images/common/icon_menu_gray.png',
  69. color: '#666',
  70. //1-选中切换,2-跳转、请求、其他操作,3-菜单
  71. type: 3,
  72. //自定义参数,类型自定义
  73. parameter: null,
  74. //子菜单left值,不传默认50%,当菜单贴近左右两边可用此参数调整
  75. popupLeft: '',
  76. itemList: [
  77. {
  78. //不建议超过6个字,请自行控制
  79. text: '自定义参',
  80. //自定义参数,类型自定义
  81. parameter: null
  82. },
  83. {
  84. text: '自定义参数',
  85. //自定义参数,类型自定义
  86. parameter: null
  87. }
  88. ]
  89. }
  90. *
  91. * */
  92. itemList: {
  93. type: Array,
  94. default: () => {
  95. return [];
  96. }
  97. },
  98. //颜色
  99. color: {
  100. type: String,
  101. default: '#666'
  102. },
  103. //选中颜色
  104. selectedColor: {
  105. type: String,
  106. default: '#5677fc'
  107. },
  108. fontSize: {
  109. type: String,
  110. default: '28rpx'
  111. },
  112. //选中后字体是否加粗
  113. bold: {
  114. type: Boolean,
  115. default: true
  116. },
  117. //导航条背景颜色
  118. backgroundColor: {
  119. type: String,
  120. default: '#F8F8F8'
  121. },
  122. //item分割线高度是否缩小
  123. splitLineScale: {
  124. type: Boolean,
  125. default: true
  126. },
  127. //二级菜单字体颜色
  128. subMenuColor: {
  129. type: String,
  130. default: '#333'
  131. },
  132. //二级菜单字体大小
  133. subMenufontSize: {
  134. type: String,
  135. default: '28rpx'
  136. },
  137. //二级菜单背景色 深色:#4c4c4c
  138. subMenuBgColor: {
  139. type: String,
  140. default: '#fff'
  141. },
  142. //二级菜单是否有点击效果
  143. subMenuHover: {
  144. type: Boolean,
  145. default: true
  146. },
  147. //是否固定在底部
  148. isFixed: {
  149. type: Boolean,
  150. default: true
  151. },
  152. //去除导航栏顶部的线条
  153. unlined: {
  154. type: Boolean,
  155. default: false
  156. },
  157. //是否暗黑模式 (true:所有设置颜色失效)
  158. isDarkMode: {
  159. type: Boolean,
  160. default: false
  161. }
  162. },
  163. data() {
  164. return {
  165. showMenuIndex: -1 //显示的菜单index
  166. };
  167. },
  168. methods: {
  169. getIcon: function(current, index, item) {
  170. let url = item.iconPath;
  171. if (item.type == 1) {
  172. url = current == index ? item.selectedIconPath || item.iconPath : item.iconPath;
  173. }
  174. return url;
  175. },
  176. stop() {
  177. return false;
  178. },
  179. handleClose() {
  180. this.showMenuIndex = -1;
  181. },
  182. menuClick(index, parameter, type) {
  183. //type:1-选中切换,2-跳转、请求、其他操作,3-菜单
  184. if (type == 3) {
  185. this.showMenuIndex = this.showMenuIndex == index ? -1 : index;
  186. } else {
  187. this.showMenuIndex = -1;
  188. this.$emit('click', {
  189. menu: 'main', //main,sub 主菜单,子菜单
  190. type: type,
  191. index: index,
  192. parameter: parameter || ''
  193. });
  194. }
  195. },
  196. subMenuClick(index, type, subIndex, parameter) {
  197. this.showMenuIndex = -1;
  198. this.$emit('click', {
  199. menu: 'sub', //main,sub 主菜单,子菜单
  200. type: type,
  201. index: index,
  202. subIndex: subIndex,
  203. parameter: parameter || ''
  204. });
  205. }
  206. }
  207. };
  208. </script>
  209. <style scoped>
  210. .tui-bottom-navigation {
  211. width: 100%;
  212. height: 100rpx;
  213. display: flex;
  214. align-items: center;
  215. justify-content: space-between;
  216. position: relative;
  217. z-index: 999;
  218. }
  219. .tui-navigation-fixed {
  220. position: fixed !important;
  221. left: 0;
  222. bottom: 0;
  223. padding-bottom: env(safe-area-inset-bottom);
  224. }
  225. .tui-bottom-navigation::after {
  226. content: '';
  227. width: 100%;
  228. border-top: 1px solid #bfbfbf;
  229. position: absolute;
  230. top: 0;
  231. left: 0;
  232. transform: scaleY(0.5) translateZ(0);
  233. transform-origin: 0 0;
  234. z-index: 1000;
  235. }
  236. .tui-remove-splitLine::before {
  237. border-top: 0 !important;
  238. }
  239. .tui-navigation-item {
  240. flex: 1;
  241. height: 100rpx;
  242. position: relative;
  243. box-sizing: border-box;
  244. }
  245. .tui-item-inner {
  246. width: 100%;
  247. height: 100rpx;
  248. display: flex;
  249. text-align: center;
  250. align-items: center;
  251. justify-content: center;
  252. }
  253. .tui-navigation-item::after {
  254. height: 100%;
  255. content: '';
  256. position: absolute;
  257. border-right: 1px solid #bfbfbf;
  258. transform: scaleX(0.5) translateZ(0);
  259. right: 0;
  260. top: 0;
  261. }
  262. .tui-item-after_height::after {
  263. height: 40% !important;
  264. top: 30% !important;
  265. }
  266. .tui-last-item::after {
  267. border-right: 0 !important;
  268. }
  269. .tui-navigation-img {
  270. width: 32rpx;
  271. height: 32rpx;
  272. margin-right: 8rpx;
  273. }
  274. .tui-navigation-popup {
  275. max-width: 160%;
  276. width: auto;
  277. position: absolute;
  278. border-radius: 8rpx;
  279. visibility: hidden;
  280. opacity: 0;
  281. transform: translate3d(-50%, 0, 0);
  282. transform-origin: center;
  283. transition: all 0.12s ease-in-out;
  284. bottom: 0;
  285. z-index: -1;
  286. }
  287. .tui-navigation-popup_show {
  288. transform: translate3d(-50%, -124rpx, 0);
  289. visibility: visible;
  290. opacity: 1;
  291. }
  292. .tui-popup-triangle {
  293. position: absolute;
  294. width: 0;
  295. height: 0;
  296. border-left: 9rpx solid transparent;
  297. border-right: 9rpx solid transparent;
  298. border-top: 18rpx solid;
  299. left: 50%;
  300. bottom: -18rpx;
  301. -webkit-transform: translateX(-50%);
  302. transform: translateX(-50%);
  303. z-index: 997;
  304. }
  305. .tui-popup-cell {
  306. width: 100%;
  307. padding: 32rpx 20rpx;
  308. box-sizing: border-box;
  309. display: flex;
  310. align-items: center;
  311. justify-content: center;
  312. flex: 1;
  313. position: relative;
  314. }
  315. .tui-ellipsis {
  316. white-space: nowrap;
  317. overflow: hidden;
  318. text-overflow: ellipsis;
  319. }
  320. .tui-popup-cell::after {
  321. content: '';
  322. position: absolute;
  323. border-bottom: 1rpx solid #eaeef1;
  324. -webkit-transform: scaleY(0.5);
  325. transform: scaleY(0.5);
  326. bottom: 0;
  327. right: 24rpx;
  328. left: 24rpx;
  329. }
  330. .tui-item-hover {
  331. background-color: #f1f1f1;
  332. }
  333. .tui-item-dark_hover {
  334. background-color: #555;
  335. }
  336. .tui-first-cell {
  337. border-top-left-radius: 8rpx;
  338. border-top-right-radius: 8rpx;
  339. }
  340. .tui-last-cell {
  341. border-bottom-left-radius: 8rpx;
  342. border-bottom-right-radius: 8rpx;
  343. }
  344. .tui-last-cell::after {
  345. border-bottom: 0 !important;
  346. }
  347. .tui-navigation-mask {
  348. position: fixed;
  349. top: 0;
  350. left: 0;
  351. right: 0;
  352. bottom: 0;
  353. z-index: 995;
  354. transition: all 0.3s ease-in-out;
  355. opacity: 0;
  356. visibility: hidden;
  357. background-color: rgba(0, 0, 0, 0);
  358. }
  359. .tui-navigation-mask_show {
  360. opacity: 1;
  361. visibility: visible;
  362. }
  363. </style>