u-index-item.vue 2.1 KB

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