index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <route lang="json5">
  2. {
  3. style: {
  4. navigationBarTitleText: '通屏+下拉刷新+自定义导航栏',
  5. enablePullDownRefresh: true,
  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. <navigator
  60. url="/pages/index/demo/throughout"
  61. open-type="navigate"
  62. hover-class="navigator-hover"
  63. >
  64. <button>去二级详情页</button>
  65. </navigator>
  66. <view class="bg-white">
  67. <fly-content :line="30" />
  68. </view>
  69. </scroll-view>
  70. </template>
  71. <script lang="ts" setup>
  72. import useNavbarWeixin from '@/hooks/useNavbarWeixin'
  73. import { onPullDownRefresh } from '@dcloudio/uni-app'
  74. const { pages, isTabbar, onScrollToLower, safeAreaInsets } = useNavbarWeixin()
  75. // 发现原生下拉刷新效果并不好,在微信里面只有顶部导航栏下拉才生效,页面区域下拉不生效,体验不好,结合自定义下拉刷新效果很好
  76. onPullDownRefresh(() => {
  77. setTimeout(function fn() {
  78. console.log('refresh - onPullDownRefresh')
  79. // 关闭动画
  80. uni.stopPullDownRefresh()
  81. }, 1000)
  82. })
  83. // 当前下拉刷新状态
  84. const isTriggered = ref(false)
  85. // 自定义下拉刷新被触发
  86. const onRefresherRefresh = async () => {
  87. // 开始动画
  88. isTriggered.value = true
  89. setTimeout(function fn() {
  90. console.log('refresh - onRefresherRefresh')
  91. // 关闭动画
  92. isTriggered.value = false
  93. }, 1000)
  94. }
  95. </script>
  96. <style lang="scss">
  97. .scroll-view-bg {
  98. // 这个背景色要与.top-section的背景图差不多,这样下拉刷新看起来才比较协调
  99. background-color: #23c09c;
  100. }
  101. // 这个区域最好要大于200rpx,效果会更好
  102. .top-section {
  103. display: flex;
  104. flex-direction: column;
  105. align-items: center;
  106. min-height: 200rpx;
  107. padding: 40rpx 0;
  108. line-height: 2;
  109. color: #fff;
  110. background-image: url('https://cip-shopping-page-0eysug01066a9e-1302818703.tcloudbaseapp.com/fly/top-bg.png');
  111. background-size: cover;
  112. }
  113. .fly-navbar {
  114. position: fixed;
  115. top: 0;
  116. left: 0;
  117. z-index: 9;
  118. width: 750rpx;
  119. color: #000;
  120. background-color: transparent;
  121. .left-icon {
  122. position: absolute;
  123. left: 0;
  124. display: flex;
  125. align-items: center;
  126. justify-content: center;
  127. width: 44px;
  128. height: 44px;
  129. font-size: 44rpx;
  130. color: #000;
  131. }
  132. .title {
  133. display: flex;
  134. align-items: center;
  135. justify-content: center;
  136. height: 44px;
  137. font-size: 32rpx;
  138. color: transparent;
  139. }
  140. }
  141. </style>