tn-index-anchor.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <!-- 支付宝小程序使用_tGetRect()获取组件的根元素尺寸,所以在外面套一个"壳" -->
  3. <view>
  4. <view :id="elId" class="tn-index-anchor__wrap" :style="[wrapperStyle]">
  5. <view class="tn-index-anchor" :class="[active ? 'tn-index-anchor--active' : '']" :style="[customAnchorStyle]">
  6. <view v-if="useSlot">
  7. <slot></slot>
  8. </view>
  9. <block v-else>
  10. <text>{{ index }}</text>
  11. </block>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'tn-index-anchor',
  19. props: {
  20. // 使用自定义内容
  21. useSlot: {
  22. type: Boolean,
  23. default: false
  24. },
  25. // 索引字符
  26. index: {
  27. type: String,
  28. default: ''
  29. },
  30. // 自定义样式
  31. customStyle: {
  32. type: Object,
  33. default() {
  34. return {}
  35. }
  36. }
  37. },
  38. computed: {
  39. customAnchorStyle() {
  40. return Object.assign(this.anchorStyle, this.customStyle)
  41. }
  42. },
  43. data() {
  44. return {
  45. elId: this.$t.uuid(),
  46. // 内容的高度
  47. height: 0,
  48. // 内容的top
  49. top: 0,
  50. // 是否被激活
  51. active: false,
  52. // 样式(父组件外部提供)
  53. wrapperStyle: {},
  54. anchorStyle: {}
  55. }
  56. },
  57. created() {
  58. this.parent = false
  59. },
  60. mounted() {
  61. this.parent = this.$t.$parent.call(this, 'tn-index-list')
  62. if (this.parent) {
  63. this.parent.childrens.push(this)
  64. this.parent.updateData()
  65. }
  66. }
  67. }
  68. </script>
  69. <style lang="scss" scoped>
  70. .tn-index-anchor {
  71. width: 100%;
  72. box-sizing: border-box;
  73. padding: 8rpx 24rpx;
  74. color: $tn-font-color;
  75. font-size: 28rpx;
  76. font-weight: 500;
  77. line-height: 1.2;
  78. background-color: rgb(245, 245, 245);
  79. &--active {
  80. right: 0;
  81. left: 0;
  82. color: $tn-main-color;
  83. background-color: #FFFFFF;
  84. }
  85. }
  86. </style>