throughout.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <route lang="json5">
  2. {
  3. style: {
  4. navigationBarTitleText: '通屏+下拉刷新+自定义导航栏',
  5. enablePullDownRefresh: false,
  6. backgroundColor: '#23c09c', // 这个背景色要与页面的.top-section的背景图差不多,这样下拉刷新看起来才比较协调
  7. 'app-plus': {
  8. titleNView: {
  9. type: 'transparent',
  10. },
  11. },
  12. 'mp-weixin': {
  13. navigationStyle: 'custom',
  14. },
  15. },
  16. }
  17. </route>
  18. <template>
  19. <!-- #ifdef MP-WEIXIN -->
  20. <view class="fly-navbar" :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
  21. <!-- 1/3,多于1个页面,用返回图标 -->
  22. <navigator v-if="pages.length > 1" open-type="navigateBack" class="left-icon">
  23. <view class="i-carbon-chevron-left text-current"></view>
  24. </navigator>
  25. <!-- 2/3,只有1个页面,如果不是tabbar,需要首页图标 -->
  26. <!-- 这种情况一般出现在用户直接打开分享出去的详情页面,或者使用redirectTo等API -->
  27. <navigator
  28. v-else-if="!isTabbar"
  29. open-type="switchTab"
  30. url="/pages/index/index"
  31. class="left-icon"
  32. >
  33. <view class="i-carbon-home text-current"></view>
  34. </navigator>
  35. <!-- 3/3,如果当前页就是tabbar页,不用去首页,也就是什么图标都不需要 -->
  36. <view class="title">{{ '我是标题' }}</view>
  37. </view>
  38. <!-- #endif -->
  39. <scroll-view
  40. enable-back-to-top
  41. scroll-y
  42. class="scroll-view-bg flex-1 h-full"
  43. id="scroller"
  44. refresher-enabled
  45. @scrolltolower="onScrollToLower"
  46. @refresherrefresh="onRefresherRefresh"
  47. :refresher-triggered="isTriggered"
  48. >
  49. <view class="top-section" :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
  50. <view class="pt-1">顶部区域</view>
  51. <view>可以是标题,也可以是个人中心头像等</view>
  52. <view>建议本区域高度不低于200rpx</view>
  53. </view>
  54. <view class="p-2 leading-6 bg-white">
  55. 注意,上面的导航栏渐变效果仅微信端支持,且上面的导航栏无法抽为组件引入使用,否则滚动效果没有了。如果不只是微信小程序使用,可以
  56. onPageScroll 实现全端效果一样,另外如果是app端,还可以配置 titleNView。参考
  57. https://uniapp.dcloud.net.cn/tutorial/page.html#onpagescroll 。
  58. </view>
  59. <view class="bg-white">
  60. <fly-content :line="30" />
  61. </view>
  62. </scroll-view>
  63. </template>
  64. <script lang="ts" setup>
  65. import useNavbarWeixin from '@/hooks/useNavbarWeixin'
  66. import { onPullDownRefresh } from '@dcloudio/uni-app'
  67. const { pages, isTabbar, onScrollToLower, safeAreaInsets } = useNavbarWeixin()
  68. // 发现原生下拉刷新效果并不好,在微信里面只有顶部导航栏下拉才生效,页面区域下拉不生效,体验不好,结合自定义下拉刷新效果很好
  69. onPullDownRefresh(() => {
  70. setTimeout(function fn() {
  71. console.log('refresh - onPullDownRefresh')
  72. // 关闭动画
  73. uni.stopPullDownRefresh()
  74. }, 1000)
  75. })
  76. // 当前下拉刷新状态
  77. const isTriggered = ref(false)
  78. // 自定义下拉刷新被触发
  79. const onRefresherRefresh = async () => {
  80. // 开始动画
  81. isTriggered.value = true
  82. setTimeout(function fn() {
  83. console.log('refresh - onRefresherRefresh')
  84. // 关闭动画
  85. isTriggered.value = false
  86. }, 1000)
  87. }
  88. </script>
  89. <style lang="scss">
  90. .scroll-view-bg {
  91. // 这个背景色要与.top-section的背景图差不多,这样下拉刷新看起来才比较协调
  92. background-color: #23c09c;
  93. }
  94. // 这个区域最好要大于200rpx,效果会更好
  95. .top-section {
  96. display: flex;
  97. flex-direction: column;
  98. align-items: center;
  99. min-height: 200rpx;
  100. padding: 40rpx 0;
  101. line-height: 2;
  102. color: #fff;
  103. background-image: url('https://cip-shopping-page-0eysug01066a9e-1302818703.tcloudbaseapp.com/fly/top-bg.png');
  104. background-size: cover;
  105. }
  106. .fly-navbar {
  107. position: fixed;
  108. top: 0;
  109. left: 0;
  110. z-index: 9;
  111. width: 750rpx;
  112. color: #000;
  113. background-color: transparent;
  114. .left-icon {
  115. position: absolute;
  116. left: 0;
  117. display: flex;
  118. align-items: center;
  119. justify-content: center;
  120. width: 44px;
  121. height: 44px;
  122. font-size: 44rpx;
  123. color: #000;
  124. }
  125. .title {
  126. display: flex;
  127. align-items: center;
  128. justify-content: center;
  129. height: 44px;
  130. font-size: 32rpx;
  131. color: transparent;
  132. }
  133. }
  134. </style>