u-list-item.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell>
  4. <!-- #endif -->
  5. <view
  6. class="u-list-item"
  7. :ref="`u-list-item-${anchor}`"
  8. :anchor="`u-list-item-${anchor}`"
  9. :class="[`u-list-item-${anchor}`]"
  10. >
  11. <slot/>
  12. </view>
  13. <!-- #ifdef APP-NVUE -->
  14. </cell>
  15. <!-- #endif -->
  16. </template>
  17. <script>
  18. import props from './props.js';
  19. // #ifdef APP-NVUE
  20. const dom = uni.requireNativePlugin('dom')
  21. // #endif
  22. /**
  23. * List 列表
  24. * @description 该组件为高性能列表组件
  25. * @tutorial https://www.uviewui.com/components/list.html
  26. * @property {String | Number} anchor 用于滚动到指定item
  27. * @example <u-list-ite v-for="(item, index) in indexList" :key="index" ></u-list-item>
  28. */
  29. export default {
  30. name: 'u-list-item',
  31. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  32. data() {
  33. return {
  34. // 节点信息
  35. rect: {},
  36. index: 0,
  37. show: true,
  38. sys: uni.$u.sys()
  39. }
  40. },
  41. computed: {},
  42. inject: ['uList'],
  43. watch: {
  44. // #ifndef APP-NVUE
  45. 'uList.innerScrollTop'(n) {
  46. const preLoadScreen = this.uList.preLoadScreen
  47. const windowHeight = this.sys.windowHeight
  48. if (n <= windowHeight * preLoadScreen) {
  49. this.parent.updateOffsetFromChild(0)
  50. } else if (this.rect.top <= n - windowHeight * preLoadScreen) {
  51. this.parent.updateOffsetFromChild(this.rect.top)
  52. }
  53. }
  54. // #endif
  55. },
  56. created() {
  57. this.parent = {}
  58. },
  59. mounted() {
  60. this.init()
  61. },
  62. methods: {
  63. init() {
  64. // 初始化数据
  65. this.updateParentData()
  66. this.index = this.parent.children.indexOf(this)
  67. this.resize()
  68. },
  69. updateParentData() {
  70. // 此方法在mixin中
  71. this.getParentData('u-list')
  72. },
  73. resize() {
  74. this.queryRect(`u-list-item-${this.anchor}`).then(size => {
  75. const lastChild = this.parent.children[this.index - 1]
  76. this.rect = size
  77. const preLoadScreen = this.uList.preLoadScreen
  78. const windowHeight = this.sys.windowHeight
  79. // #ifndef APP-NVUE
  80. if (lastChild) {
  81. this.rect.top = lastChild.rect.top + lastChild.rect.height
  82. }
  83. if (size.top >= this.uList.innerScrollTop + (1 + preLoadScreen) * windowHeight) this.show =
  84. false
  85. // #endif
  86. })
  87. },
  88. // 查询元素尺寸
  89. queryRect(el) {
  90. return new Promise(resolve => {
  91. // #ifndef APP-NVUE
  92. this.$uGetRect(`.${el}`).then(size => {
  93. resolve(size)
  94. })
  95. // #endif
  96. // #ifdef APP-NVUE
  97. const ref = this.$refs[el]
  98. dom.getComponentRect(ref, res => {
  99. resolve(res.size)
  100. })
  101. // #endif
  102. })
  103. }
  104. }
  105. }
  106. </script>
  107. <style lang="scss" scoped>
  108. @import "../../libs/css/components.scss";
  109. .u-list-item {
  110. }
  111. </style>