uv-index-anchor.vue 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <header>
  4. <!-- #endif -->
  5. <view
  6. :class="['uv-index-anchor',{'uv-index-anchor-sticky': parentData.sticky}]"
  7. :ref="`uv-index-anchor-${text}`"
  8. :style="{
  9. height: $uv.addUnit(height),
  10. backgroundColor: bgColor
  11. }"
  12. >
  13. <text
  14. class="uv-index-anchor__text"
  15. :style="{
  16. fontSize: $uv.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 mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  27. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  28. import props from './props.js';
  29. // #ifdef APP-NVUE
  30. const dom = uni.requireNativePlugin('dom')
  31. // #endif
  32. /**
  33. * IndexAnchor 列表锚点
  34. * @description
  35. * @tutorial https://www.uvui.cn/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 <uv-index-anchor :text="indexList[index]"></uv-index-anchor>
  42. */
  43. export default {
  44. name: 'uv-index-anchor',
  45. mixins: [mpMixin, mixin, props],
  46. data() {
  47. return {
  48. parentData: {
  49. sticky: true
  50. }
  51. }
  52. },
  53. created() {
  54. this.init()
  55. },
  56. methods: {
  57. init() {
  58. // 此处会活动父组件实例,并赋值给实例的parent属性
  59. const indexList = this.$uv.$parent.call(this, 'uv-index-list')
  60. if (!indexList) {
  61. return this.$uv.error('uv-index-anchor必须要搭配uv-index-list组件使用')
  62. }
  63. this.parentData.sticky = indexList.sticky;
  64. // 将当前实例放入到uv-index-list中
  65. indexList.anchors.push(this)
  66. const indexListItem = this.$uv.$parent.call(this, 'uv-index-item')
  67. // #ifndef APP-NVUE
  68. // 只有在非nvue下,uv-index-anchor才是嵌套在uv-index-item中的
  69. if (!indexListItem) {
  70. return this.$uv.error('uv-index-anchor必须要搭配uv-index-item组件使用')
  71. }
  72. // 设置uv-index-item的id为anchor的text标识符,因为非nvue下滚动列表需要依赖scroll-view滚动到元素的特性
  73. indexListItem.id = this.text.charCodeAt(0)
  74. // #endif
  75. }
  76. },
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  81. .uv-index-anchor {
  82. @include flex;
  83. align-items: center;
  84. padding-left: 15px;
  85. z-index: 1;
  86. &-sticky {
  87. position: sticky;
  88. top: 0;
  89. }
  90. &__text {
  91. @include flex;
  92. align-items: center;
  93. }
  94. }
  95. </style>