3
0

u-index-item.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell ref="u-index-item">
  4. <!-- #endif -->
  5. <view
  6. class="u-index-item"
  7. :id="`u-index-item-${id}`"
  8. :class="[`u-index-item-${id}`]"
  9. >
  10. <slot />
  11. </view>
  12. <!-- #ifdef APP-NVUE -->
  13. </cell>
  14. <!-- #endif -->
  15. </template>
  16. <script>
  17. import { props } from './props';
  18. import { mpMixin } from '../../libs/mixin/mpMixin';
  19. import { mixin } from '../../libs/mixin/mixin';
  20. import { sleep, error } from '../../libs/function/index';
  21. // #ifdef APP-NVUE
  22. // 由于weex为阿里的KPI业绩考核的产物,所以不支持百分比单位,这里需要通过dom查询组件的宽度
  23. const dom = uni.requireNativePlugin('dom')
  24. // #endif
  25. /**
  26. * IndexItem
  27. * @description
  28. * @tutorial https://uview-plus.jiangruyi.com/components/indexList.html
  29. * @property {String}
  30. * @event {Function}
  31. * @example
  32. */
  33. export default {
  34. name: 'u-index-item',
  35. mixins: [mpMixin, mixin, props],
  36. data() {
  37. return {
  38. // 本组件到滚动条顶部的距离
  39. top: 0,
  40. height: 0,
  41. id: ''
  42. }
  43. },
  44. created() {
  45. // 子组件u-index-anchor的实例
  46. this.anchor = {}
  47. },
  48. mounted() {
  49. this.init()
  50. },
  51. methods: {
  52. init() {
  53. // 此处会活动父组件实例,并赋值给实例的parent属性
  54. this.getParentData('u-index-list')
  55. if (!this.parent) {
  56. return error('u-index-item必须要搭配u-index-list组件使用')
  57. }
  58. sleep().then(() =>{
  59. this.getIndexItemRect().then(size => {
  60. // 由于对象的引用特性,此处会同时生效到父组件的children数组的本实例的top属性中,供父组件判断读取
  61. this.top = Math.ceil(size.top)
  62. this.height = Math.ceil(size.height)
  63. })
  64. })
  65. },
  66. getIndexItemRect() {
  67. return new Promise(resolve => {
  68. // #ifndef APP-NVUE
  69. this.$uGetRect('.u-index-item').then(size => {
  70. resolve(size)
  71. })
  72. // #endif
  73. // #ifdef APP-NVUE
  74. const ref = this.$refs['u-index-item']
  75. dom.getComponentRect(ref, res => {
  76. resolve(res.size)
  77. })
  78. // #endif
  79. })
  80. }
  81. },
  82. }
  83. </script>
  84. <style lang="scss" scoped>
  85. @import "../../libs/css/components.scss";
  86. </style>