nested-scroll-body.uvue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <scroll-view style="flex:1" type="nested" direction="vertical" refresher-enabled="true" refresher-default-style="none"
  3. bounces="false" :refresher-triggered="refresherTriggered" @refresherpulling="onRefresherpulling"
  4. @refresherrefresh="onRefresherrefresh" @refresherrestore="onRefreshrestore" @scrollend="scrollEnd">
  5. <nested-scroll-header>
  6. <swiper ref="header" indicator-dots="true" circular="true">
  7. <swiper-item v-for="i in 3" :item-id="i">
  8. <image src="/static/shuijiao.jpg" style="width:100% ;height: 240px;"></image>
  9. </swiper-item>
  10. </swiper>
  11. </nested-scroll-header>
  12. <nested-scroll-body>
  13. <view style="flex:1">
  14. <view style="flex-direction: row;">
  15. <text style="padding: 12px 15px;">nested-scroll-body</text>
  16. </view>
  17. <!-- 嵌套滚动仅可能关闭bounces效果 会影响嵌套滚动不连贯 -->
  18. <list-view bounces="false" id="body-list" :scroll-top="scrollTop" style="flex:1"
  19. associative-container="nested-scroll-view" :refresher-enabled="false">
  20. <list-item v-for="key in scrollData">
  21. <view class="scroll-item">
  22. <text class="scroll-item-title">{{key}}</text>
  23. </view>
  24. </list-item>
  25. </list-view>
  26. </view>
  27. <text>不会渲染,因为 nested-scroll-body 只会渲染第一个子节点</text>
  28. </nested-scroll-body>
  29. <!-- 支持自定义样式下拉刷新slot组件 -->
  30. <refresh-box slot="refresher" :state="state"></refresh-box>
  31. </scroll-view>
  32. </template>
  33. <script>
  34. import refreshBox from '../../template/custom-refresher/refresh-box/refresh-box.uvue';
  35. export default {
  36. components: { refreshBox },
  37. data() {
  38. return {
  39. scrollData: [] as Array<string>,
  40. scrollTop: 0,
  41. refresherTriggered: false,
  42. pullingDistance: 0,
  43. resetting: false,
  44. testScrollTop: 0
  45. }
  46. },
  47. computed: {
  48. state() : number {
  49. if (this.resetting) {
  50. return 3;
  51. }
  52. if (this.refresherTriggered) {
  53. return 2
  54. }
  55. if (this.pullingDistance > 45) {
  56. return 1
  57. } else {
  58. return 0;
  59. }
  60. }
  61. },
  62. onLoad() {
  63. let lists : Array<string> = []
  64. for (let i = 0; i < 30; i++) {
  65. lists.push("item---" + i)
  66. }
  67. this.scrollData = lists
  68. },
  69. methods: {
  70. onRefresherpulling(e : RefresherEvent) {
  71. this.pullingDistance = e.detail.dy;
  72. },
  73. onRefresherrefresh() {
  74. this.refresherTriggered = true
  75. setTimeout(() => {
  76. this.refresherTriggered = false
  77. this.resetting = true;
  78. }, 1500)
  79. },
  80. onRefreshrestore() {
  81. this.pullingDistance = 0
  82. this.resetting = false;
  83. },
  84. //自动化测试使用
  85. testBodyScrollBy(y : number) {
  86. this.scrollTop = y
  87. },
  88. scrollEnd(e : UniScrollEvent) {
  89. this.testScrollTop = e.detail.scrollTop
  90. console.log('testScrollTop:', this.testScrollTop)
  91. }
  92. }
  93. }
  94. </script>
  95. <style>
  96. .scroll-item {
  97. margin-left: 6px;
  98. margin-right: 6px;
  99. margin-top: 6px;
  100. background-color: #fff;
  101. border-radius: 4px;
  102. }
  103. .scroll-item-title {
  104. width: 100%;
  105. height: 60px;
  106. line-height: 60px;
  107. text-align: center;
  108. color: #555;
  109. }
  110. .scroll-header-tiem {
  111. height: 200px;
  112. background-color: #66ccff;
  113. align-items: center;
  114. justify-content: center;
  115. }
  116. </style>