uv-index-item.vue 2.1 KB

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