fs-tab.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <view class="text-center" :class="[sticky ? 'fs-tab-sticky' : '', gutter ? 'fs-tab-gutter' : '']">
  3. <scroll-view :scroll-x="scrollable" :style="{ 'background-color': bgColor }">
  4. <view class="fs-tab" :class="['fs-tab-' + type, colorType, round ? 'round' : '', center ? 'fs-tab-center' : '']">
  5. <view
  6. class="fs-tab-item"
  7. :class="[type + '-item', type + '-item-' + colorType, { active: index == curIndex }]"
  8. :style="itemStyle"
  9. v-for="(item, index) in tabs"
  10. :key="index"
  11. @click="setActive(index)"
  12. >
  13. <slot :item="item" :index="index">{{ item.name }}</slot>
  14. <view v-if="type === 'line' && index == curIndex" class="fs-tab-item-bar" :style="{ width: barWidth }"></view>
  15. </view>
  16. </view>
  17. </scroll-view>
  18. </view>
  19. </template>
  20. <script>
  21. /**
  22. * 标签页组件
  23. * @description 标签页组件
  24. * @property {String} type = [line | card] 类型
  25. * @property {Array} tabs 列表
  26. * @property {String} colorType 高亮颜色类型
  27. * @property {String} bgColor 背景颜色
  28. * @property {String} barWidth 指示线宽度
  29. * @property {Number} scrollThreshold 滑动阈值
  30. * @property {Boolean} center 是否居中
  31. * @property {Boolean} round 半角
  32. * @property {Boolean} sticky 是否吸顶
  33. * @property {Boolean} gutter 下边距
  34. * @event {Function} change change事件
  35. */
  36. export default {
  37. name: 'fs-tab'
  38. }
  39. </script>
  40. <script setup>
  41. import { reactive, computed } from 'vue'
  42. const props = defineProps({
  43. colorType: {
  44. type: String,
  45. default: 'primary',
  46. validator(value) {
  47. return ['primary', 'success', 'info', 'warning', 'danger'].includes(value)
  48. }
  49. },
  50. bgColor: {
  51. type: String,
  52. default: '#fff'
  53. },
  54. barWidth: {
  55. type: String,
  56. default: '100%'
  57. },
  58. scrollThreshold: {
  59. type: Number,
  60. default: 5
  61. },
  62. type: {
  63. type: String,
  64. default: 'line',
  65. validator(value) {
  66. return ['line', 'card'].includes(value)
  67. }
  68. },
  69. tabs: {
  70. type: Array,
  71. default() {
  72. return []
  73. }
  74. },
  75. center: Boolean,
  76. round: Boolean,
  77. sticky: Boolean,
  78. gutter: Boolean,
  79. modelValue: {
  80. type: [String, Number],
  81. default: 0
  82. }
  83. })
  84. const emits = defineEmits(['change', 'update:modelValue'])
  85. const scrollable = computed(() => props.scrollThreshold <= props.tabs.length)
  86. const itemStyle = computed(() => (scrollable.value ? `flex: 0 0 ${88 / props.scrollThreshold}%;` : ''))
  87. const curIndex = computed(() => props.modelValue)
  88. const setActive = index => {
  89. emits('update:modelValue', index)
  90. emits('change', index)
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. .fs-tab {
  95. display: flex;
  96. height: 90rpx;
  97. line-height: 90rpx;
  98. background-color: #fff;
  99. text-align: center;
  100. white-space: nowrap;
  101. &-line {
  102. &.primary .active {
  103. color: var(--primary);
  104. }
  105. &.danger .active {
  106. color: var(--danger);
  107. }
  108. &.warning .active {
  109. color: var(--warning);
  110. }
  111. &.success .active {
  112. color: var(--success);
  113. }
  114. &.info .active {
  115. color: var(--info);
  116. }
  117. &::after {
  118. content: '';
  119. position: absolute;
  120. bottom: 0;
  121. left: 0;
  122. width: 200%;
  123. transform: scale(0.5);
  124. transform-origin: 0 0;
  125. height: 1px;
  126. background-color: var(--border-color);
  127. }
  128. }
  129. &-card {
  130. border: 2rpx solid var(--primary);
  131. border-radius: var(--radius);
  132. overflow: hidden;
  133. &.round {
  134. border-radius: 30px;
  135. }
  136. &.danger {
  137. border-color: var(--danger);
  138. }
  139. &.warning {
  140. border-color: var(--warning);
  141. }
  142. &.info {
  143. border-color: var(--info);
  144. }
  145. &.success {
  146. border-color: var(--success);
  147. }
  148. & .active {
  149. color: #fff;
  150. }
  151. &.primary .active {
  152. background-color: var(--primary);
  153. }
  154. &.danger .active {
  155. background-color: var(--danger);
  156. }
  157. &.warning .active {
  158. background-color: var(--warning);
  159. }
  160. &.success .active {
  161. background-color: var(--success);
  162. }
  163. &.info .active {
  164. background-color: var(--info);
  165. }
  166. }
  167. &-center {
  168. display: inline-flex;
  169. width: auto;
  170. & .fs-tab-item {
  171. padding: 0 30px;
  172. }
  173. }
  174. &-sticky {
  175. position: sticky;
  176. top: 0;
  177. z-index: 10;
  178. }
  179. &-gutter {
  180. margin-bottom: var(--gutter);
  181. }
  182. }
  183. .fs-tab-item {
  184. flex: 1;
  185. position: relative;
  186. box-sizing: border-box;
  187. color: var(--content);
  188. &-bar {
  189. position: absolute;
  190. bottom: 0;
  191. height: 2px;
  192. background-color: currentColor;
  193. width: 100%;
  194. left: 50%;
  195. transform: translateX(-50%);
  196. animation: width 0.5s;
  197. z-index: 10;
  198. }
  199. }
  200. .card-item-primary + .card-item-primary {
  201. border-left: 1px solid var(--primary);
  202. }
  203. .card-item-danger + .card-item-danger {
  204. border-left: 1px solid var(--danger);
  205. }
  206. .card-item-warning + .card-item-warning {
  207. border-left: 1px solid var(--warning);
  208. }
  209. .card-item-success + .card-item-success {
  210. border-left: 1px solid var(--success);
  211. }
  212. .card-item-info + .card-item-info {
  213. border-left: 1px solid var(--info);
  214. }
  215. @keyframes width {
  216. 0% {
  217. transform: translateX(-50%) scale(0);
  218. opacity: 0;
  219. }
  220. 100% {
  221. transform: translateX(-50%) scale(1);
  222. }
  223. }
  224. /* #ifdef H5 */
  225. // 通过样式穿透,隐藏H5下,scroll-view下的滚动条
  226. scroll-view ::v-deep ::-webkit-scrollbar {
  227. display: none;
  228. width: 0 !important;
  229. height: 0 !important;
  230. -webkit-appearance: none;
  231. background: transparent;
  232. }
  233. ::v-deep .fs-tab .uni-scroll-view-content {
  234. display: flex;
  235. }
  236. /* #endif */
  237. </style>