u-index-anchor.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <header>
  4. <!-- #endif -->
  5. <view
  6. class="u-index-anchor u-border-bottom"
  7. :class="{ 'u-index-anchor--sticky': parentSticky }"
  8. :ref="`u-index-anchor-${text}`"
  9. :style="{
  10. height: addUnit(height),
  11. backgroundColor: bgColor
  12. }"
  13. >
  14. <text
  15. class="u-index-anchor__text"
  16. :style="{
  17. fontSize: addUnit(size),
  18. color: color
  19. }"
  20. >{{ text.name || text }}</text>
  21. </view>
  22. <!-- #ifdef APP-NVUE -->
  23. </header>
  24. <!-- #endif -->
  25. </template>
  26. <script>
  27. import { props } from './props';
  28. import { mpMixin } from '../../libs/mixin/mpMixin';
  29. import { mixin } from '../../libs/mixin/mixin';
  30. import { addUnit, $parent, error } from '../../libs/function/index';
  31. // #ifdef APP-NVUE
  32. const dom = uni.requireNativePlugin('dom')
  33. // #endif
  34. /**
  35. * IndexAnchor 列表锚点
  36. * @description
  37. * @tutorial https://uview-plus.jiangruyi.com/components/indexList.html
  38. * @property {String | Number} text 列表锚点文本内容
  39. * @property {String} color 列表锚点文字颜色 ( 默认 '#606266' )
  40. * @property {String | Number} size 列表锚点文字大小,单位默认px ( 默认 14 )
  41. * @property {String} bgColor 列表锚点背景颜色 ( 默认 '#dedede' )
  42. * @property {String | Number} height 列表锚点高度,单位默认px ( 默认 32 )
  43. * @example <u-index-anchor :text="indexList[index]"></u-index-anchor>
  44. */
  45. export default {
  46. name: 'u-index-anchor',
  47. mixins: [mpMixin, mixin, props],
  48. data() {
  49. return {
  50. }
  51. },
  52. mounted() {
  53. this.init()
  54. },
  55. methods: {
  56. addUnit,
  57. init() {
  58. // 此处会活动父组件实例,并赋值给实例的parent属性
  59. const indexList = $parent.call(this, 'u-index-list')
  60. if (!indexList) {
  61. return error('u-index-anchor必须要搭配u-index-list组件使用')
  62. }
  63. // 将当前实例放入到u-index-list中
  64. indexList.anchors.push(this)
  65. const indexListItem = $parent.call(this, 'u-index-item')
  66. // #ifndef APP-NVUE
  67. // 只有在非nvue下,u-index-anchor才是嵌套在u-index-item中的
  68. if (!indexListItem) {
  69. return error('u-index-anchor必须要搭配u-index-item组件使用')
  70. }
  71. // 设置u-index-item的id为anchor的text标识符,因为非nvue下滚动列表需要依赖scroll-view滚动到元素的特性
  72. if (typeof this.text == 'string') {
  73. indexListItem.id = this.text.charCodeAt(0)
  74. } else {
  75. indexListItem.id = this.text.name.charCodeAt(0)
  76. }
  77. // #endif
  78. }
  79. },
  80. computed: {
  81. parentSticky() {
  82. const indexList = $parent.call(this, "u-index-list");
  83. return indexList ? indexList.sticky : true;
  84. },
  85. },
  86. }
  87. </script>
  88. <style lang="scss" scoped>
  89. .u-index-anchor {
  90. position: sticky;
  91. top: 0;
  92. @include flex;
  93. align-items: center;
  94. padding-left: 15px;
  95. z-index: 1;
  96. &--sticky {
  97. position: sticky;
  98. top: 0;
  99. }
  100. &__text {
  101. @include flex;
  102. align-items: center;
  103. }
  104. }
  105. </style>