u-index-anchor.vue 2.4 KB

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