sun-tab.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view :class="{'uni-scroll-tab': scroll === true }" class="uni-tab">
  3. <view
  4. v-for="(tab,index) in tabList" :key="index"
  5. :class="{ 'uni-tab-active': index === value, 'uni-tab-scroll-item': scroll === true, ' uni-tab-scroll-active': index === value && scroll === true }"
  6. :style="[{color:index === value ? activeColor : defaultColor,backgroundColor: bgColor}]"
  7. @tap="itemClick(index,tab)"
  8. class="uni-tab-item">
  9. <span v-if="tab.icon != undefined" class="iconfont mr5" :class="tab.icon"></span>
  10. <text>{{rangeKey == '' ? tab : tab[rangeKey]}}</text>
  11. </view>
  12. <view v-if="!scroll" :style="[{ right: barRight + '%', left : barLeft + '%', borderColor: activeColor }]" class="uni-tab-bar" :class="back ? 'uni-tab-bar-backward' : 'uni-tab-bar-forward'"></view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'uni-tab',
  18. data() {
  19. return {
  20. average: 0,
  21. back: false
  22. };
  23. },
  24. props: {
  25. value: {
  26. type: Number, //当前选中下标
  27. default () {
  28. return 0;
  29. }
  30. },
  31. tabList: {
  32. type: Array,
  33. default () {
  34. return [];
  35. }
  36. },
  37. bgColor: { //背景颜色
  38. type: String,
  39. default () {
  40. return '#FFFFFF';
  41. }
  42. },
  43. defaultColor: { //默认未选中文字颜色
  44. type: String,
  45. default () {
  46. return '#000000';
  47. }
  48. },
  49. activeColor: { //选中时文字颜色 线条颜色
  50. type: String,
  51. default () {
  52. return '#1e9fff';
  53. }
  54. },
  55. rangeKey: { // 当tabList为对象时 显示指定下标值
  56. type: String,
  57. default () {
  58. return '';
  59. }
  60. },
  61. scroll: { //横向滑动
  62. type: Boolean,
  63. default () {
  64. return false;
  65. }
  66. },
  67. },
  68. computed:{
  69. barLeft () {
  70. return this.value * this.average;
  71. },
  72. barRight () {
  73. let index = this.tabList.length - this.value - 1;
  74. return index * this.average;
  75. },
  76. },
  77. created() {
  78. this.average = 100 / this.tabList.length;
  79. },
  80. methods: {
  81. itemClick(index,tab){
  82. if(this.value == index) return false;
  83. if(this.value > index){
  84. this.back = true;
  85. }else{
  86. this.back = false;
  87. }
  88. // this.value = index;
  89. this.$emit('update:value', index);
  90. this.$emit('change',{tab:tab});
  91. }
  92. }
  93. };
  94. </script>
  95. <style lang="scss" scoped>
  96. // @import "../../static/icon/iconfont.css";
  97. .uni-tab{
  98. position: relative;
  99. display: flex;
  100. font-size: 14px;
  101. height: 44px;
  102. line-height: 44px;
  103. background-color: #fff;
  104. .uni-tab-item{
  105. flex: 1;
  106. height: 100%;
  107. text-align: center;
  108. box-sizing: border-box;
  109. overflow: hidden;
  110. }
  111. .uni-tab-scroll-item{
  112. flex: none;
  113. padding: 0px 12px;
  114. }
  115. .uni-tab-active{
  116. color: #1e9fff;
  117. }
  118. .uni-tab-scroll-active{
  119. border-bottom: 3px solid #1e9fff;
  120. }
  121. .uni-tab-bar{
  122. display: block;
  123. height: 3px;
  124. position: absolute;
  125. bottom: 0;
  126. border-bottom: 3px solid #1e9fff;
  127. }
  128. .uni-tab-bar-forward{
  129. transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1), left 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s;
  130. }
  131. .uni-tab-bar-backward{
  132. transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s, left 0.3s cubic-bezier(0.35, 0, 0.25, 1);
  133. }
  134. }
  135. .uni-scroll-tab{
  136. overflow-x: scroll;
  137. }
  138. </style>