u-list-item.vue 2.6 KB

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