智慧画屏客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.4 KiB

3 years ago
  1. <template>
  2. <view :class="{ text: styleType === 'text' }"
  3. :style="{ borderColor: styleType === 'text' ? '' : activeColor }"
  4. class="segmented-control">
  5. <view v-for="(item, index) in values"
  6. :class="[{ text: styleType === 'text' }, { active: index === currentIndex }]"
  7. :key="index" :style="{
  8. color:
  9. index === currentIndex
  10. ? styleType === 'text'
  11. ? activeColor
  12. : 'rgb(255,255,255)'
  13. : styleType === 'text'
  14. ? 'rgb(135,156,196)'
  15. : activeColor,
  16. backgroundColor: index === currentIndex && styleType === 'button' ? activeColor : ''
  17. }" class="segmented-control-item" @click="_onClick(index)">
  18. {{ item }}
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'UniSegmentedControl',
  25. props: {
  26. current: {
  27. type: Number,
  28. default: 0
  29. },
  30. values: {
  31. type: Array,
  32. default () {
  33. return []
  34. }
  35. },
  36. activeColor: {
  37. type: String,
  38. default: 'rgb(255,255,255)'
  39. },
  40. styleType: {
  41. type: String,
  42. default: 'button'
  43. }
  44. },
  45. data() {
  46. return {
  47. currentIndex: 0
  48. }
  49. },
  50. watch: {
  51. current(val) {
  52. if (val !== this.currentIndex) {
  53. this.currentIndex = val
  54. }
  55. }
  56. },
  57. created() {
  58. this.currentIndex = this.current
  59. },
  60. methods: {
  61. _onClick(index) {
  62. if (this.currentIndex !== index) {
  63. this.currentIndex = index
  64. this.$emit('clickItem', index)
  65. }
  66. }
  67. }
  68. }
  69. </script>
  70. <style>
  71. @charset "UTF-8";
  72. .segmented-control {
  73. display: flex;
  74. flex-direction: row;
  75. justify-content: center;
  76. width: 75%;
  77. font-size: 38upx;
  78. box-sizing: border-box;
  79. margin: 0 auto;
  80. overflow: hidden;
  81. border: 1px solid;
  82. border-radius: 10upx
  83. }
  84. .segmented-control.text {
  85. border: 0;
  86. border-radius: 0
  87. }
  88. .segmented-control-item {
  89. flex: 1;
  90. text-align: center;
  91. line-height: 60upx;
  92. box-sizing: border-box;
  93. border-left: 1px solid
  94. }
  95. .segmented-control-item.active {
  96. color: white;
  97. position: relative;
  98. }
  99. .segmented-control-item.text {
  100. border-left: 0;
  101. font-size: 0.7rem;
  102. padding-bottom:5upx;
  103. }
  104. .segmented-control-item.text.active:after {
  105. content:"";
  106. display: block;
  107. position: absolute;
  108. left: 36%;
  109. bottom: -4upx;
  110. height: 10upx;
  111. width: 27%;
  112. background:url(../../static/img/caitubiao.png) no-repeat;
  113. background-size:100%;
  114. }
  115. .segmented-control-item:first-child {
  116. border-left-width: 0
  117. }
  118. </style>