uv-vtabs-item.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <view
  3. class="uv-vtabs-item"
  4. :id="`content_${index}`"
  5. ref="uv-vtabs-item"
  6. >
  7. <slot />
  8. </view>
  9. </template>
  10. <script>
  11. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js';
  12. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js';
  13. // #ifdef APP-NVUE
  14. const dom = uni.requireNativePlugin('dom');
  15. // #endif
  16. export default {
  17. name: 'uv-vtabs-item',
  18. mixins: [mpMixin, mixin],
  19. props: {
  20. index: {
  21. type: [Number,String],
  22. default: 0
  23. }
  24. },
  25. data(){
  26. return {
  27. // 记录item的离顶部的距离
  28. top: 0,
  29. // 记录item的高度
  30. height: 0
  31. // 是否为联动
  32. }
  33. },
  34. mounted() {
  35. this.init();
  36. },
  37. methods: {
  38. async init(){
  39. this.getParentData('uv-vtabs');
  40. if (!this.parent) {
  41. return this.$uv.error('uv-vtabs必须要搭配uv-vtabs-item组件使用')
  42. }
  43. if(!this.parent.chain) return;
  44. await this.$uv.sleep();
  45. this.getItemRect().then(size=>{
  46. // 由于对象的引用特性,此处会同时生效到父组件的children数组的本实例的top属性中,供父组件判断读取
  47. this.top = size.top;
  48. this.height = size.height;
  49. });
  50. },
  51. getItemRect(){
  52. return new Promise(resolve => {
  53. // #ifndef APP-NVUE
  54. this.$uvGetRect('.uv-vtabs-item').then(size => {
  55. resolve(size)
  56. })
  57. // #endif
  58. // #ifdef APP-NVUE
  59. const ref = this.$refs['uv-vtabs-item']
  60. dom.getComponentRect(ref, res => {
  61. resolve(res.size)
  62. })
  63. // #endif
  64. })
  65. }
  66. }
  67. }
  68. </script>