index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <script >
  2. import LinearText from '../LinearText';
  3. export default {
  4. name: 'ButtonBlock',
  5. components: {
  6. LinearText
  7. },
  8. props: {
  9. items: {
  10. type: Array,
  11. default: () => [
  12. "本日",
  13. "本周",
  14. "本月"
  15. ]
  16. },
  17. valueField: {
  18. type: String,
  19. default: ""
  20. },
  21. defaultIndex: {
  22. type: Number,
  23. default: 0
  24. }
  25. },
  26. data() {
  27. return {
  28. currentItemIndex: 0,
  29. }
  30. },
  31. emits: ["select-item"],
  32. created() {
  33. this.currentItemIndex = this.defaultIndex;
  34. },
  35. methods: {
  36. /**
  37. * 渲染当前节点的颜色
  38. * @param {*} index 当前选项索引
  39. * @param {*} color 选择时的颜色
  40. */
  41. renderCurrentItemColor(index, color) {
  42. return this.currentItemIndex === index ? color : `${color}88`
  43. },
  44. /**
  45. * 渲染文本内容
  46. * @param {*} item 节点数据
  47. */
  48. renderText(item) {
  49. if (this.valueField === '') {
  50. return item
  51. } else {
  52. return item[this.valueField]
  53. }
  54. },
  55. /**
  56. * 点击节点处理器
  57. * @param {*} index 当前点击的选项索引
  58. */
  59. onClickBlockItemHandler(index) {
  60. this.currentItemIndex = index
  61. this.$emit('select-item', index)
  62. }
  63. }
  64. }
  65. </script>
  66. <template >
  67. <div class="button-block">
  68. <div class="button-block_item" v-for="(item,index) in items" :key="index">
  69. <linear-text :text="renderText(item)"
  70. :startColor="renderCurrentItemColor(index, '#95CCFF')"
  71. :endColor="renderCurrentItemColor(index, '#FFFFFF')"
  72. fontSize="16px" :ext-class="{
  73. textShadow: `0 1px 1px ${renderCurrentItemColor(index, '#0057FF')}`
  74. }" @click="onClickBlockItemHandler(index)" />
  75. </div>
  76. </div>
  77. </template>
  78. <style scoped lang='less'>
  79. .button-block {
  80. display: flex;
  81. border-radius: 5px;
  82. box-sizing: border-box;
  83. background: linear-gradient(
  84. 360deg,
  85. #0094FF60 100%,
  86. #0094FF00 0%
  87. );
  88. .button-block_item {
  89. padding: 0px 8.5px 0px 8.5px;
  90. cursor: pointer;
  91. }
  92. &::after {
  93. content: "";
  94. position: absolute;
  95. top: 0px;
  96. left: 0px;
  97. padding: 1px;
  98. box-sizing: border-box;
  99. background: linear-gradient(360deg, #0c5a87 0%, rgba(0, 163, 255, 0) 100%);
  100. /* 随便定义颜色 */
  101. --mask-bg: linear-gradient(red, red);
  102. --m-o: content-box, padding-box;
  103. /* mask允许使用者通过遮罩或者裁切特定区域的图片的方式来隐藏一个元素的部分或者全部可见区域 */
  104. /* 设置了用作元素蒙版层的图像,默认值为none,值为透明图片,或透明渐变 */
  105. -webkit-mask-image: var(--mask-bg), var(--mask-bg);
  106. /* 默认值为border-box,可选值与background-origin相同 */
  107. -webkit-mask-origin: var(--m-o);
  108. /* 默认值为border-box,可选值与background-clip相同 */
  109. -webkit-mask-clip: var(--m-o);
  110. /* exclude排除,只显示不重合的地方,Firefox支持4个属性 */
  111. mask-composite: exclude;
  112. /*只显示下方遮罩,重合的地方不显示*/
  113. -webkit-mask-composite: destination-out;
  114. pointer-events: none;
  115. }
  116. }
  117. </style>