scroll-fold-nav.uvue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <template>
  2. <scroll-view @scroll="onScroll" class="page" show-scrollbar="false">
  3. <view ref="seatbar" class="seatbar"></view>
  4. <view class="content">
  5. <view class="content-item">
  6. <text class="text">1. 当前示例监听了 scroll-view 的 scroll 事件 ,滚动页面实时监听scrollTop。</text>
  7. <text class="text">2. 使用 ref 直接获取元素的节点,并在 scroll 事件中通过节点的 setProperty
  8. 方法来修改搜索导航栏的高度、位置和背景颜色等样式,从而达到滚动折叠的效果。</text>
  9. <text class="text">3. 请向上\向下滚动页面观察效果。</text>
  10. </view>
  11. <view class="content-item" v-for="(item,index) in 20" :key="index">
  12. <text class="text">content-{{item}}</text>
  13. </view>
  14. </view>
  15. <view ref="navigatorbar" class="navigatorbar">
  16. <view class="titlebar">
  17. <view class="backview" @tap="back">
  18. <image class="back" src="/static/template/scroll-fold-nav/back.png" mode="widthFix"></image>
  19. </view>
  20. <text ref="title" class="title">DCloud 为开发者而生</text>
  21. </view>
  22. <view ref="searchbar" class="searchbar" @tap="search">
  23. <image class="searchimg" src="/static/template/scroll-fold-nav/search.png" mode="widthFix"></image>
  24. <text class="searchinput">请输入你要搜索的内容</text>
  25. <text class="searchbutton">搜索</text>
  26. </view>
  27. </view>
  28. </scroll-view>
  29. </template>
  30. <script>
  31. //导航栏高度
  32. const NAVIBARHEIGHT = 88;
  33. //搜索栏高度
  34. const SEARCHBARHEIGHT = 40;
  35. //返回键按钮宽度
  36. const BACKWIDTH = 32;
  37. export default {
  38. data() {
  39. return {
  40. statusBarHeight: 0,
  41. nviBarHeight: 0,
  42. naviElement: null as UniElement | null,
  43. titleElement: null as UniElement | null,
  44. searchElement: null as UniElement | null,
  45. seatElement: null as UniElement | null
  46. }
  47. },
  48. onLoad() {
  49. // #ifdef APP || MP
  50. this.statusBarHeight = uni.getWindowInfo().statusBarHeight;
  51. // #endif
  52. this.nviBarHeight = NAVIBARHEIGHT + this.statusBarHeight;
  53. },
  54. onReady() {
  55. this.naviElement = this.$refs['navigatorbar'] as UniElement;
  56. this.searchElement = this.$refs['searchbar'] as UniElement;
  57. this.titleElement = this.$refs['title'] as UniElement;
  58. this.seatElement = this.$refs['seatbar'] as UniElement;
  59. this.setStyle();
  60. },
  61. onResize(_ : OnResizeOptions) {
  62. // #ifdef APP-ANDROID
  63. // 监听多窗口模式
  64. this.statusBarHeight = uni.getWindowInfo().statusBarHeight;
  65. this.nviBarHeight = NAVIBARHEIGHT + this.statusBarHeight;
  66. this.setStyle();
  67. // #endif
  68. },
  69. methods: {
  70. onScroll(e : ScrollEvent) {
  71. let offset = e.detail.scrollTop>SEARCHBARHEIGHT?SEARCHBARHEIGHT:e.detail.scrollTop;//(e.detail.scrollTop<0?0:e.detail.scrollTop)
  72. this.naviElement?.style?.setProperty('height', (this.nviBarHeight -offset)+'px');
  73. this.titleElement?.style?.setProperty('opacity', (1-offset/SEARCHBARHEIGHT).toString());
  74. this.searchElement?.style?.setProperty('left', ((offset<0)?0:BACKWIDTH*offset/SEARCHBARHEIGHT)+'px');
  75. },
  76. back() {
  77. uni.navigateBack();
  78. },
  79. search() {
  80. uni.showToast({
  81. title: '暂不支持',
  82. icon: 'none'
  83. });
  84. },
  85. setStyle() {
  86. this.naviElement?.style?.setProperty('padding-top', this.statusBarHeight + 'px');
  87. this.naviElement?.style?.setProperty('height', (NAVIBARHEIGHT + this.statusBarHeight) + 'px');
  88. this.seatElement?.style?.setProperty('height', (NAVIBARHEIGHT + this.statusBarHeight) + 'px');
  89. }
  90. }
  91. }
  92. </script>
  93. <style>
  94. .page {
  95. flex: 1;
  96. background-color: #f5f5f5;
  97. }
  98. .navigatorbar {
  99. position: fixed;
  100. /* #ifdef APP */
  101. padding-top: 35px;
  102. height: 124px;
  103. /* #endif */
  104. /* #ifdef WEB */
  105. height: 88px;
  106. /* #endif */
  107. border-bottom: 1px solid #efefef;
  108. width: 100%;
  109. background-color: #f5f5f5;
  110. }
  111. .titlebar {
  112. flex-direction: row;
  113. align-items: center;
  114. height: 44px;
  115. }
  116. .backview {
  117. width: 44px;
  118. height: 44px;
  119. justify-content: center;
  120. align-items: center;
  121. }
  122. .back {
  123. width: 20px;
  124. }
  125. .title {
  126. margin: 0px 2px;
  127. }
  128. .searchbar {
  129. position: absolute;
  130. bottom: 2px;
  131. left: 0px;
  132. right: 0px;
  133. background-color: #FFFFFF;
  134. border: 1px solid #fbdf0d;
  135. height: 32px;
  136. border-radius: 100px;
  137. margin: 6px 12px;
  138. padding: 8px;
  139. flex-direction: row;
  140. align-items: center;
  141. justify-content: center;
  142. }
  143. .searchimg {
  144. width: 15px;
  145. }
  146. .searchinput {
  147. flex-grow: 1;
  148. font-size: 12px;
  149. color: #666;
  150. }
  151. .searchbutton {
  152. font-size: 12px;
  153. background-color: #ff6900;
  154. color: #FFF;
  155. padding: 5px 8px;
  156. border-radius: 100px;
  157. }
  158. .seatbar {
  159. /* #ifdef APP */
  160. height: 124px;
  161. /* #endif */
  162. /* #ifdef WEB */
  163. height: 88px;
  164. /* #endif */
  165. }
  166. .content {
  167. padding: 5px 15px;
  168. }
  169. .content-item {
  170. padding: 15px;
  171. margin: 5px 0;
  172. background-color: #fff;
  173. border-radius: 5px;
  174. }
  175. .text {
  176. font-size: 14px;
  177. color: #666;
  178. line-height: 20px;
  179. }
  180. </style>