su-tab.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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.createSelectorQuery()
  186. .in(vm)
  187. .select('#tab-' + vm.uid)
  188. .boundingClientRect((data) => {
  189. if (data != null) {
  190. if (data.left == 0 && data.right == 0) {
  191. // setTimeout(() => {
  192. computedQuery();
  193. // }, 300);
  194. } else {
  195. state.content = data;
  196. setTimeout(() => {
  197. state.over = true;
  198. }, 300);
  199. }
  200. } else {
  201. console.log('tab-' + vm.uid + ' data error');
  202. }
  203. })
  204. .exec();
  205. };
  206. const setCurValue = (value) => {
  207. if (value == state.curValue) return;
  208. state.curValue = value;
  209. computedMark();
  210. };
  211. const click = (index, item) => {
  212. setCurValue(index);
  213. emits('update:modelValue', index);
  214. emits('change', {
  215. index: index,
  216. data: item,
  217. });
  218. };
  219. const upitem = (index, e) => {
  220. state.tabNodeList[index] = e;
  221. if (index == state.curValue) {
  222. computedMark();
  223. }
  224. };
  225. const computedMark = () => {
  226. if (state.tabNodeList.length == 0) return;
  227. let left = 0;
  228. let list = unref(state.tabNodeList);
  229. let cur = state.curValue;
  230. state.markLeft = list[cur].left - state.content.left;
  231. state.markWidth = list[cur].width;
  232. };
  233. const computedScroll = () => {
  234. if (state.curValue == 0 || state.curValue == state.tabNodeList.length - 1) {
  235. return false;
  236. }
  237. let i = 0;
  238. let left = 0;
  239. let list = state.tabNodeList;
  240. for (i in list) {
  241. if (i == state.curValue && i != 0) {
  242. left = left - list[i - 1].width;
  243. break;
  244. }
  245. left = left + list[i].width;
  246. }
  247. state.scrollLeft = left;
  248. };
  249. provide('suTabProvide', {
  250. props,
  251. curValue: toRef(state, 'curValue'),
  252. });
  253. </script>
  254. <style lang="scss">
  255. .ui-tab {
  256. position: relative;
  257. display: flex;
  258. height: 4em;
  259. align-items: center;
  260. &.ui-tab-scrolls {
  261. width: 100%;
  262. /* #ifdef MP-WEIXIN */
  263. padding-bottom: 10px;
  264. /* #endif */
  265. .ui-tab-scroll-warp {
  266. overflow: hidden;
  267. height: inherit;
  268. width: 100%;
  269. .ui-tab-scroll {
  270. position: relative;
  271. display: block;
  272. white-space: nowrap;
  273. overflow: auto;
  274. min-height: 4em;
  275. line-height: 4em;
  276. width: 100% !important;
  277. .ui-tab-mark-warp {
  278. display: flex;
  279. align-items: top;
  280. justify-content: center;
  281. .ui-tab-mark.ui-btn {
  282. /* #ifndef MP-WEIXIN */
  283. height: 2em;
  284. width: calc(100% - 0.6em);
  285. margin-top: 4px;
  286. /* #endif */
  287. /* #ifdef MP-WEIXIN */
  288. height: 2em;
  289. width: calc(100% - 0.6em);
  290. margin-top: 4px;
  291. /* #endif */
  292. }
  293. }
  294. }
  295. }
  296. }
  297. .ui-tab-mark-warp {
  298. color: inherit;
  299. position: absolute;
  300. top: 0;
  301. height: 100%;
  302. z-index: 0;
  303. &.over {
  304. transition: 0.3s;
  305. }
  306. .ui-tab-mark {
  307. color: var(--ui-BG-Main);
  308. height: 100%;
  309. }
  310. }
  311. &.line {
  312. .ui-tab-mark {
  313. border-bottom: 2px solid currentColor;
  314. }
  315. }
  316. &.topline {
  317. .ui-tab-mark {
  318. border-top: 2px solid currentColor;
  319. }
  320. }
  321. &.dot {
  322. .ui-tab-mark::after {
  323. content: '';
  324. width: 0.5em;
  325. height: 0.5em;
  326. background-color: currentColor;
  327. border-radius: 50%;
  328. display: block;
  329. position: absolute;
  330. bottom: 0.3em;
  331. left: 0;
  332. right: 0;
  333. margin: auto;
  334. }
  335. }
  336. &.long {
  337. .ui-tab-mark::after {
  338. content: '';
  339. width: 2em;
  340. height: 0.35em;
  341. background-color: currentColor;
  342. border-radius: 5em;
  343. display: block;
  344. position: absolute;
  345. bottom: 0.3em;
  346. left: 0;
  347. right: 0;
  348. margin: auto;
  349. }
  350. }
  351. &.trapezoid {
  352. .ui-tab-mark::after {
  353. content: '';
  354. width: calc(100% - 2em);
  355. height: 0.35em;
  356. background-color: currentColor;
  357. border-radius: 5em 5em 0 0;
  358. display: block;
  359. position: absolute;
  360. bottom: 0;
  361. left: 0;
  362. right: 0;
  363. margin: auto;
  364. }
  365. }
  366. &.btn {
  367. .ui-tab-mark-warp {
  368. display: flex;
  369. align-items: center;
  370. justify-content: center;
  371. .ui-tab-mark.ui-btn {
  372. height: calc(100% - 1.6em);
  373. width: calc(100% - 0.6em);
  374. }
  375. }
  376. &.sm .ui-tab-mark.ui-btn {
  377. height: calc(100% - 2px);
  378. width: calc(100% - 2px);
  379. border-radius: #{$radius - 2};
  380. }
  381. }
  382. &.subtitle {
  383. .ui-tab-mark-warp {
  384. display: flex;
  385. align-items: top;
  386. justify-content: center;
  387. padding-top: 0.6em;
  388. .ui-tab-mark.ui-btn {
  389. height: calc(100% - 2.8em);
  390. width: calc(100% - 0.6em);
  391. }
  392. }
  393. }
  394. &.ui-tab-inline {
  395. display: inline-flex;
  396. height: 3.5em;
  397. &.ui-tab-scrolls {
  398. .ui-tab-scroll {
  399. height: calc(3.5em + 17px);
  400. line-height: 3.5em;
  401. .ui-tab-mark-warp {
  402. height: 3.5em;
  403. }
  404. }
  405. }
  406. &.btn {
  407. .ui-tab-mark-warp {
  408. .ui-tab-mark.ui-btn {
  409. height: calc(100% - 10px);
  410. width: calc(100% - 10px);
  411. }
  412. }
  413. }
  414. }
  415. &.sm {
  416. height: 70rpx !important;
  417. &.ui-tab-inline {
  418. height: 70rpx;
  419. &.ui-tab-scrolls {
  420. .ui-tab-scroll {
  421. height: calc(70rpx + 17px);
  422. line-height: 70rpx;
  423. .ui-tab-mark-warp {
  424. height: 70rpx;
  425. }
  426. }
  427. }
  428. &.btn .ui-tab-mark.ui-btn {
  429. height: calc(100% - 2px);
  430. width: calc(100% - 2px);
  431. border-radius: #{$radius - 2};
  432. }
  433. }
  434. }
  435. }
  436. </style>