综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sticky-data.vue 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!-- 菜单悬浮的原理: 通过给菜单添加position:sticky实现, 用法超简单, 仅APP端的低端机不兼容 https://caniuse.com/#feat=css-sticky -->
  2. <template>
  3. <view>
  4. <!-- 对于mescroll-body: 需设置:sticky="true", 此应避免在mescroll-body标签前面加其他非定位的元素, 否则下拉区域会被挤出, 无法会隐藏.-->
  5. <!-- 对于mescroll-uni: 则无需设置:sticky="true", 无其他限制和要求 -->
  6. <mescroll-body :sticky="true" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
  7. <swiper style="min-height: 300rpx" autoplay="true" interval="3000" duration="300" circular="true">
  8. <swiper-item>
  9. <image style="width: 100%;height: auto;" src="https://www.mescroll.com/img/swiper1.jpg" mode="widthFix"/>
  10. </swiper-item>
  11. <swiper-item>
  12. <image style="width: 100%;height: auto;" src="https://www.mescroll.com/img/swiper2.jpg" mode="widthFix"/>
  13. </swiper-item>
  14. </swiper>
  15. <view class="demo-tip">
  16. <view>列表只初始化一次,切换菜单缓存数据</view>
  17. <view>吸顶通过给菜单加position:sticky实现, 用法简单</view>
  18. <view>小程序,微信h5端: 低端机sticky也可生效, 可放心使用</view>
  19. <view>APP端: 仅部分低端机无效,若要兼容则参考sticky-scroll</view>
  20. </view>
  21. <!-- sticky吸顶悬浮的菜单, 父元素必须是 mescroll -->
  22. <view class="sticky-tabs">
  23. <me-tabs v-model="tabIndex" :tabs="tabs" @change="tabChange"></me-tabs>
  24. </view>
  25. <!-- 数据列表 -->
  26. <good-list :list="goods"></good-list>
  27. </mescroll-body>
  28. <!-- 此处可以写其他fixed定位元素 -->
  29. <!-- <view></view> -->
  30. </view>
  31. </template>
  32. <script>
  33. import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
  34. import {apiSearch} from "@/api/mock.js"
  35. export default {
  36. mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
  37. data() {
  38. return {
  39. tabs:[
  40. {name:'全部', goods: null, num:1, y:0, curPageLen:0, hasNext:true},
  41. {name:'母婴', goods: null, num:1, y:0, curPageLen:0, hasNext:true},
  42. {name:'图书', goods: null, num:1, y:0, curPageLen:0, hasNext:true}
  43. ],
  44. tabIndex: 0 // 当前菜单下标
  45. }
  46. },
  47. computed: {
  48. // 列表数据
  49. goods() {
  50. return this.tabs[this.tabIndex].goods
  51. }
  52. },
  53. methods: {
  54. /*下拉刷新的回调 */
  55. downCallback() {
  56. // 这里加载你想下拉刷新的数据, 比如刷新轮播数据
  57. // loadSwiper();
  58. // 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
  59. this.mescroll.resetUpScroll()
  60. },
  61. /*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
  62. upCallback(page) {
  63. let keyword = this.tabs[this.tabIndex].name;
  64. apiSearch(page.num, page.size, keyword).then(curPageData=>{
  65. // 当前tab数据
  66. let curTab = this.tabs[this.tabIndex]
  67. // 设置列表数据
  68. if(page.num == 1) curTab.goods = []; //如果是第一页需手动制空列表
  69. curTab.goods = curTab.goods.concat(curPageData); //追加新数据
  70. curTab.num = page.num; // 页码
  71. curTab.curPageLen = curPageData.length; // 当前页长
  72. curTab.hasNext = this.mescroll.optUp.hasNext; // 是否还有下一页
  73. // 需先隐藏加载状态
  74. this.mescroll.endSuccess(curPageData.length);
  75. }).catch(()=>{
  76. //联网失败, 结束加载
  77. this.mescroll.endErr();
  78. })
  79. },
  80. // 切换菜单
  81. tabChange (index) {
  82. // 记录切换前滚动条的位置
  83. if(!this.preIndex) this.preIndex = 0
  84. let preTab = this.tabs[this.preIndex]
  85. preTab.y = this.mescroll.getScrollTop()
  86. this.preIndex = index;
  87. // 当前菜单的数据
  88. let curTab = this.tabs[index]
  89. if (!curTab.goods) {
  90. // 没有初始化,则初始化
  91. this.mescroll.resetUpScroll()
  92. } else{
  93. // 初始化过,则恢复之前的列表数据
  94. this.mescroll.setPageNum(curTab.num + 1); // 恢复当前页码
  95. this.mescroll.endSuccess(curTab.curPageLen, curTab.hasNext); // 恢复是否有下一页或显示空布局
  96. this.$nextTick(()=>{
  97. this.mescroll.scrollTo(curTab.y, 0) // 恢复滚动条的位置
  98. })
  99. }
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss">
  105. /*
  106. sticky生效条件:
  107. 1、父元素不能overflow:hidden或者overflow:auto属性。(mescroll-body设置:sticky="true"即可, mescroll-uni本身没有设置overflow)
  108. 2、必须指定top、bottom、left、right4个值之一,否则只会处于相对定位
  109. 3、父元素的高度不能低于sticky元素的高度
  110. 4、sticky元素仅在其父元素内生效,所以父元素必须是 mescroll
  111. */
  112. .sticky-tabs{
  113. z-index: 990;
  114. position: sticky;
  115. top: var(--window-top);
  116. background-color: #fff;
  117. }
  118. // 使用mescroll-uni,则top为0
  119. .mescroll-uni,
  120. /deep/.mescroll-uni{
  121. .sticky-tabs{
  122. top: 0;
  123. }
  124. }
  125. .demo-tip{
  126. padding: 18rpx;
  127. font-size: 24rpx;
  128. text-align: center;
  129. }
  130. </style>