myNews.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <!-- 滑动切换选项卡+吸顶演示(上一个tab数据不保留,滚动流畅) -->
  2. <template>
  3. <view class="content">
  4. <z-paging ref="paging" refresher-only @onRefresh="onRefresh" :refresher-status.sync="refresherStatus" @scrolltolower="scrolltolower">
  5. <view>
  6. <!-- 小程序中直接修改组件style为position: sticky;无效,需要在组件外层套一层view -->
  7. <view style="z-index: 100;position: sticky;top :0;">
  8. <tabs-view @change="tabsChange" :current="current" :items="tabList"></tabs-view>
  9. </view>
  10. <swiper class="swiper" :style="[{height:swiperHeight+'px'}]" :current="current" @animationfinish="animationfinish">
  11. <swiper-item class="swiper-item" v-for="(item, index) in tabList" :key="index">
  12. <my-news-list ref="swiperList" :phone="userInfo.phone" :tabIndex="index" :currentIndex="current" @heightChanged="heightChanged">
  13. </my-news-list>
  14. </swiper-item>
  15. </swiper>
  16. </view>
  17. </z-paging>
  18. </view>
  19. </template>
  20. <script>
  21. import { mapState } from 'vuex'
  22. export default {
  23. data() {
  24. return {
  25. refresherStatus: 0,
  26. swiperHeight: 0,
  27. tabList: ['已发布', '待审核'],
  28. current: 0, // tabs组件的current值,表示当前活动的tab选项
  29. }
  30. },
  31. computed: {
  32. ...mapState(['userInfo'])
  33. },
  34. methods: {
  35. //tabs通知swiper切换
  36. tabsChange(index) {
  37. this._setCurrent(index);
  38. },
  39. //下拉刷新时,通知当前显示的列表进行reload操作
  40. onRefresh(){
  41. this.$refs.swiperList[this.current].reload(() => {
  42. this.$refs.paging.complete([]);
  43. });
  44. },
  45. //当滚动到底部时,通知当前显示的列表加载更多
  46. scrolltolower(){
  47. this.$refs.swiperList[this.current].doLoadMore();
  48. },
  49. // swiper滑动结束
  50. animationfinish(e) {
  51. let current = e.detail.current;
  52. this._setCurrent(current);
  53. },
  54. //设置swiper的高度
  55. heightChanged(height){
  56. if(height === 0){
  57. //默认swiper高度为屏幕可用高度-tabsView高度-slot="top"内view的高度
  58. height = uni.getSystemInfoSync().windowHeight - uni.upx2px(80);
  59. }
  60. this.swiperHeight = height;
  61. },
  62. _setCurrent(current){
  63. if (current !== this.current){
  64. //切换tab时,将上一个tab的数据清空
  65. this.$refs.swiperList[this.current].clear();
  66. }
  67. this.current = current;
  68. }
  69. }
  70. }
  71. </script>
  72. <style>
  73. .banner-view {
  74. background-color: #007AFF;
  75. color: white;
  76. display: flex;
  77. flex-direction: column;
  78. align-items: center;
  79. justify-content: center;
  80. }
  81. .paging-content {
  82. flex: 1;
  83. display: flex;
  84. flex-direction: column;
  85. }
  86. .swiper {
  87. height: 1000px;
  88. }
  89. </style>