su-tab.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <view
  3. class="ui-tab"
  4. ref="tabRef"
  5. :id="'tab-' + vm.uid"
  6. :class="[
  7. props.ui,
  8. props.tpl,
  9. props.bg,
  10. props.align,
  11. { 'ui-tab-inline': props.inline },
  12. { 'ui-tab-scrolls': props.scroll },
  13. ]"
  14. >
  15. <block v-if="scroll">
  16. <view class="ui-tab-scroll-warp">
  17. <scroll-view
  18. scroll-x="true"
  19. class="ui-tab-scroll"
  20. :scroll-left="state.curValue > 1 ? state.tabNodeList[state.curValue - 1].left : 0"
  21. scroll-with-animation
  22. :style="{ width: `${state.content.width}px` }"
  23. >
  24. <view class="ss-flex ss-col-center">
  25. <su-tab-item
  26. v-for="(item, index) in props.tab"
  27. :data="item"
  28. :index="index"
  29. :key="index"
  30. @up="upitem"
  31. @tap.native="click(index, item)"
  32. ></su-tab-item>
  33. <view
  34. class="ui-tab-mark-warp"
  35. :class="[{ over: state.over }]"
  36. :style="[{ left: state.markLeft + 'px' }, { width: state.markWidth + 'px' }]"
  37. >
  38. <view
  39. class="ui-tab-mark"
  40. :class="[props.mark, { 'ui-btn': props.tpl == 'btn' || props.tpl == 'subtitle' }]"
  41. :style="[
  42. {
  43. background:
  44. props.tpl == 'btn' || props.tpl == 'subtitle' ? titleStyle.activeBg : 'none',
  45. },
  46. ]"
  47. ></view>
  48. </view>
  49. </view>
  50. </scroll-view>
  51. </view>
  52. </block>
  53. <block v-else>
  54. <su-tab-item
  55. v-for="(item, index) in props.tab"
  56. :data="item"
  57. :index="index"
  58. :key="index"
  59. @up="upitem"
  60. @tap.native="click(index, item)"
  61. ></su-tab-item>
  62. <view
  63. class="ui-tab-mark-warp"
  64. :class="[{ over: state.over }]"
  65. :style="[{ left: state.markLeft + 'px' }, { width: state.markWidth + 'px' }]"
  66. >
  67. <view
  68. class="ui-tab-mark"
  69. :class="[props.mark, { 'ui-btn': props.tpl == 'btn' || props.tpl == 'subtitle' }]"
  70. ></view>
  71. </view>
  72. </block>
  73. </view>
  74. </template>
  75. <script>
  76. export default {
  77. name: 'SuTab',
  78. };
  79. </script>
  80. <script setup>
  81. /**
  82. * 基础组件 - suTab
  83. */
  84. import {
  85. toRef,
  86. ref,
  87. reactive,
  88. unref,
  89. onMounted,
  90. nextTick,
  91. getCurrentInstance,
  92. provide,
  93. } from 'vue';
  94. const vm = getCurrentInstance();
  95. // 数据
  96. const state = reactive({
  97. curValue: 0,
  98. tabNodeList: [],
  99. scrollLeft: 0,
  100. markLeft: 0,
  101. markWidth: 0,
  102. content: {
  103. width: 100,
  104. },
  105. over: false,
  106. });
  107. const tabRef = ref(null);
  108. // 参数
  109. const props = defineProps({
  110. modelValue: {
  111. type: Number,
  112. default: 0,
  113. },
  114. ui: {
  115. type: String,
  116. default: '',
  117. },
  118. bg: {
  119. type: String,
  120. default: '',
  121. },
  122. tab: {
  123. type: Array,
  124. default() {
  125. return [];
  126. },
  127. },
  128. // line dot long,subtitle,trapezoid
  129. tpl: {
  130. type: String,
  131. default: 'line',
  132. },
  133. mark: {
  134. type: String,
  135. default: '',
  136. },
  137. align: {
  138. type: String,
  139. default: '',
  140. },
  141. curColor: {
  142. type: String,
  143. default: 'ui-TC',
  144. },
  145. defaultColor: {
  146. type: String,
  147. default: 'ui-TC',
  148. },
  149. scroll: {
  150. type: Boolean,
  151. default: false,
  152. },
  153. inline: {
  154. type: Boolean,
  155. default: false,
  156. },
  157. titleStyle: {
  158. type: Object,
  159. default: () => ({
  160. activeBg: '#DA2B10',
  161. activeColor: '#FEFEFE',
  162. color: '#D70000',
  163. }),
  164. },
  165. subtitleStyle: {
  166. type: Object,
  167. default: () => ({
  168. activeColor: '#333',
  169. color: '#C42222',
  170. }),
  171. },
  172. });
  173. const emits = defineEmits(['update:modelValue', 'change']);
  174. onMounted(() => {
  175. state.curValue = props.modelValue;
  176. setCurValue(props.modelValue);
  177. nextTick(() => {
  178. computedQuery();
  179. });
  180. uni.onWindowResize((res) => {
  181. computedQuery();
  182. });
  183. });
  184. const computedQuery = () => {
  185. uni
  186. .createSelectorQuery()
  187. .in(vm)
  188. .select('#tab-' + vm.uid)
  189. .boundingClientRect((data) => {
  190. if (data != null) {
  191. if (data.left == 0 && data.right == 0) {
  192. // setTimeout(() => {
  193. computedQuery();
  194. // }, 300);
  195. } else {
  196. state.content = data;
  197. setTimeout(() => {
  198. state.over = true;
  199. }, 300);
  200. }
  201. } else {
  202. console.log('tab-' + vm.uid + ' data error');
  203. }
  204. })
  205. .exec();
  206. };
  207. const setCurValue = (value) => {
  208. if (value == state.curValue) return;
  209. state.curValue = value;
  210. computedMark();
  211. };
  212. const click = (index, item) => {
  213. setCurValue(index);
  214. emits('update:modelValue', index);
  215. emits('change', {
  216. index: index,
  217. data: item,
  218. });
  219. };
  220. const upitem = (index, e) => {
  221. state.tabNodeList[index] = e;
  222. if (index == state.curValue) {
  223. computedMark();
  224. }
  225. };
  226. const computedMark = () => {
  227. if (state.tabNodeList.length == 0) return;
  228. let left = 0;
  229. let list = unref(state.tabNodeList);
  230. let cur = state.curValue;
  231. state.markLeft = list[cur].left - state.content.left;
  232. state.markWidth = list[cur].width;
  233. };
  234. const computedScroll = () => {
  235. if (state.curValue == 0 || state.curValue == state.tabNodeList.length - 1) {
  236. return false;
  237. }
  238. let i = 0;
  239. let left = 0;
  240. let list = state.tabNodeList;
  241. for (i in list) {
  242. if (i == state.curValue && i != 0) {
  243. left = left - list[i - 1].width;
  244. break;
  245. }
  246. left = left + list[i].width;
  247. }
  248. state.scrollLeft = left;
  249. };
  250. provide('suTabProvide', {
  251. props,
  252. curValue: toRef(state, 'curValue'),
  253. });
  254. </script>
  255. <style lang="scss">
  256. .ui-tab {
  257. position: relative;
  258. display: flex;
  259. height: 4em;
  260. align-items: center;
  261. &.ui-tab-scrolls {
  262. width: 100%;
  263. /* #ifdef MP-WEIXIN */
  264. padding-bottom: 10px;
  265. /* #endif */
  266. .ui-tab-scroll-warp {
  267. overflow: hidden;
  268. height: inherit;
  269. width: 100%;
  270. .ui-tab-scroll {
  271. position: relative;
  272. display: block;
  273. white-space: nowrap;
  274. overflow: auto;
  275. min-height: 4em;
  276. line-height: 4em;
  277. width: 100% !important;
  278. .ui-tab-mark-warp {
  279. display: flex;
  280. align-items: top;
  281. justify-content: center;
  282. .ui-tab-mark.ui-btn {
  283. /* #ifndef MP-WEIXIN */
  284. height: 2em;
  285. width: calc(100% - 0.6em);
  286. margin-top: 4px;
  287. /* #endif */
  288. /* #ifdef MP-WEIXIN */
  289. height: 2em;
  290. width: calc(100% - 0.6em);
  291. margin-top: 4px;
  292. /* #endif */
  293. }
  294. }
  295. }
  296. }
  297. }
  298. .ui-tab-mark-warp {
  299. color: inherit;
  300. position: absolute;
  301. top: 0;
  302. height: 100%;
  303. z-index: 0;
  304. &.over {
  305. transition: 0.3s;
  306. }
  307. .ui-tab-mark {
  308. color: var(--ui-BG-Main);
  309. height: 100%;
  310. }
  311. }
  312. &.line {
  313. .ui-tab-mark {
  314. border-bottom: 2px solid currentColor;
  315. }
  316. }
  317. &.topline {
  318. .ui-tab-mark {
  319. border-top: 2px solid currentColor;
  320. }
  321. }
  322. &.dot {
  323. .ui-tab-mark::after {
  324. content: '';
  325. width: 0.5em;
  326. height: 0.5em;
  327. background-color: currentColor;
  328. border-radius: 50%;
  329. display: block;
  330. position: absolute;
  331. bottom: 0.3em;
  332. left: 0;
  333. right: 0;
  334. margin: auto;
  335. }
  336. }
  337. &.long {
  338. .ui-tab-mark::after {
  339. content: '';
  340. width: 2em;
  341. height: 0.35em;
  342. background-color: currentColor;
  343. border-radius: 5em;
  344. display: block;
  345. position: absolute;
  346. bottom: 0.3em;
  347. left: 0;
  348. right: 0;
  349. margin: auto;
  350. }
  351. }
  352. &.trapezoid {
  353. .ui-tab-mark::after {
  354. content: '';
  355. width: calc(100% - 2em);
  356. height: 0.35em;
  357. background-color: currentColor;
  358. border-radius: 5em 5em 0 0;
  359. display: block;
  360. position: absolute;
  361. bottom: 0;
  362. left: 0;
  363. right: 0;
  364. margin: auto;
  365. }
  366. }
  367. &.btn {
  368. .ui-tab-mark-warp {
  369. display: flex;
  370. align-items: center;
  371. justify-content: center;
  372. .ui-tab-mark.ui-btn {
  373. height: calc(100% - 1.6em);
  374. width: calc(100% - 0.6em);
  375. }
  376. }
  377. &.sm .ui-tab-mark.ui-btn {
  378. height: calc(100% - 2px);
  379. width: calc(100% - 2px);
  380. border-radius: #{$radius - 2};
  381. }
  382. }
  383. &.subtitle {
  384. .ui-tab-mark-warp {
  385. display: flex;
  386. align-items: top;
  387. justify-content: center;
  388. padding-top: 0.6em;
  389. .ui-tab-mark.ui-btn {
  390. height: calc(100% - 2.8em);
  391. width: calc(100% - 0.6em);
  392. }
  393. }
  394. }
  395. &.ui-tab-inline {
  396. display: inline-flex;
  397. height: 3.5em;
  398. &.ui-tab-scrolls {
  399. .ui-tab-scroll {
  400. height: calc(3.5em + 17px);
  401. line-height: 3.5em;
  402. .ui-tab-mark-warp {
  403. height: 3.5em;
  404. }
  405. }
  406. }
  407. &.btn {
  408. .ui-tab-mark-warp {
  409. .ui-tab-mark.ui-btn {
  410. height: calc(100% - 10px);
  411. width: calc(100% - 10px);
  412. }
  413. }
  414. }
  415. }
  416. &.sm {
  417. height: 70rpx !important;
  418. &.ui-tab-inline {
  419. height: 70rpx;
  420. &.ui-tab-scrolls {
  421. .ui-tab-scroll {
  422. height: calc(70rpx + 17px);
  423. line-height: 70rpx;
  424. .ui-tab-mark-warp {
  425. height: 70rpx;
  426. }
  427. }
  428. }
  429. &.btn .ui-tab-mark.ui-btn {
  430. height: calc(100% - 2px);
  431. width: calc(100% - 2px);
  432. border-radius: #{$radius - 2};
  433. }
  434. }
  435. }
  436. }
  437. </style>