u-index-anchor.vue 2.5 KB

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