u-index-anchor.vue 2.5 KB

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