scroll-fold-nav.uvue 5.5 KB
Newer Older
雪洛's avatar
雪洛 已提交
1
<template>
雪洛's avatar
雪洛 已提交
2
  <scroll-view @scroll="onScroll" class="scroll-view" rebound="false">
雪洛's avatar
雪洛 已提交
3 4 5
    <!--#ifdef APP-->
    <view class="height-seat" style="height: 125px;">
      <!-- 垫高专用 -->
雪洛's avatar
雪洛 已提交
6 7 8 9 10 11 12
    </view>
    <!--#endif-->
    <!--#ifdef WEB-->
    <view class="height-seat" style="height: 90px;">
      <!-- 垫高专用 -->
    </view>
    <!--#endif-->
雪洛's avatar
雪洛 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    <view class="content">
      <view class="content-item">
        <text class="text">1. 当前示例监听了 scroll-view 的 scroll 事件 ,滚动页面实时监听scrollTop。</text>
        <text class="text">2. 使用 ref 直接获取元素的节点,并在 scroll 事件中通过节点的 setProperty
          方法来修改搜索导航栏的高度、位置和背景颜色等样式,从而达到滚动折叠的效果。</text>
        <text class="text">3. 请向上\向下滚动页面观察效果。</text>
      </view>
      <view class="content-item" v-for="(item,index) in 20" :key="index">
        <text class="text">content-{{item}}</text>
      </view>
    </view>

    <view ref="top-box" class="top-box">
      <view ref="scroll-fold-nav" class="scroll-fold-nav">
        <view class="nav-title">DCloud 为开发者而生</view>
      </view>
      <view @click="back" class="nav-back">
        <image class="back-img" src="/static/template/scroll-fold-nav/back.png" mode="widthFix"></image>
      </view>
      <view ref="search" class="search" @click="toSearchPage">
        <view class="search-inner">
          <image class="search-img" src="/static/template/scroll-fold-nav/search.png" mode="widthFix"></image>
          <text class="search-tip-text">请输入你要搜索的内容</text>
        </view>
        <text class="search-btn">搜索</text>
      </view>
    </view>

  </scroll-view>
</template>

<script>
  export default {
    data() {
      return {
        statusBarHeight: 35,
        scrollTop: 0,
        searchWidth: 700,
        searchNode: null as UniElement | null,
        boxNode: null as UniElement | null,
        navNode: null as UniElement | null
      }
    },
    onLoad() { },
    onReady() {
      this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight ?? 35;
      this.searchNode = this.$refs['search'] as UniElement;
      this.boxNode = this.$refs['top-box'] as UniElement;
      this.navNode = this.$refs['scroll-fold-nav'] as UniElement;
    },
    methods: {
      onScroll(e : ScrollEvent) {
雪洛's avatar
雪洛 已提交
66 67 68 69 70
        let scrollTop = e.detail.scrollTop
        // #ifdef APP
        const originalBoxHeight = 125
        // #else
        const originalBoxHeight = 90
雪洛's avatar
雪洛 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        // #endif
        this.boxNode?.style?.setProperty('height', (originalBoxHeight - (scrollTop > 35 ? 35 : scrollTop)) + 'px');
        this.boxNode?.style?.setProperty('background-color', 'rgba(255, 255, 255, ' + (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100.0 + ')');
        this.navNode?.style?.setProperty('opacity', 1 - (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100.0);
        this.searchNode?.style?.setProperty('width', 700 - (scrollTop > 40 ? 40 : scrollTop) + 'rpx');
        this.searchNode?.style?.setProperty('top', 0 - (scrollTop > 40 ? 40 : scrollTop) + 'px');
        this.scrollTop = scrollTop;
      },
      back() {
        // uni.navigateBack()  // 这么写用不了
        // 这么写可以用
        uni.navigateBack({
          success(result) {
            console.log('navigateBack success', result.errMsg)
          },
          fail(error) {
            console.log('navigateBack fail', error.errMsg)
          },
          complete(result) {
            console.log('navigateBack complete', result.errMsg)
          },
        })

        // uni.switchTab({
        //   url:'/pages/tabBar/template'
        // })

      },
      toSearchPage() {
        uni.showToast({
          title: '暂不支持',
          icon: 'none'
        });
      }
    },

  }
</script>

<style>
  .page {
    flex: 1;
    background-color: #fbdf0d;
  }

  .scroll-view {
    flex: 1;
  }

  .height-seat {
    height: 110px;
    /* background-color: #fbdf0d; */
  }

  .content {
    padding: 5px 15px;
    background-color: #f5f5f5;
  }

  .content-item {
    padding: 15px;
    margin: 5px 0;
    background-color: #fff;
    border-radius: 5px;
  }

  .text {
    font-size: 14px;
    color: #666;
    line-height: 20px;
  }

  .top-box {
    position: fixed;
    top: 0;
    width: 100%;
    align-items: flex-end;
雪洛's avatar
雪洛 已提交
148
    border-bottom: 1px solid #efefef;
雪洛's avatar
雪洛 已提交
149 150
    /* #ifdef APP */
    height: 125px;
雪洛's avatar
雪洛 已提交
151 152 153
    padding-top: 35px;
    /* #else */
    height: 90px;
雪洛's avatar
雪洛 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    /* #endif */
    background-color: rgba(255, 255, 255, 0);
    background-color: aliceblue;
  }

  .scroll-fold-nav {
    height: 44px;
    width: 375px;
    justify-content: center;
  }

  .nav-title {
    margin-left: 30px;
  }

  .nav-back {
雪洛's avatar
雪洛 已提交
170
    position: absolute;
雪洛's avatar
雪洛 已提交
171
    /* #ifdef APP */
雪洛's avatar
雪洛 已提交
172 173 174
    top: 46px;
    /* #else */
    top: 11px;
雪洛's avatar
雪洛 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    /* #endif */
    left: 8px;
  }

  .nav-back .back-img {
    width: 18px;
    margin-top: 2px;
  }

  .search {
    background-color: #FFFFFF;
    border: 1px solid #fbdf0d;
    height: 35px;
    border-radius: 100px;
    margin: 0 12px;
    padding: 8px;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    width: 350px;
    top: 0px;
  }

  .search-inner {
    margin-top: 2px;
    flex-direction: row;
  }

  .search-inner .search-img {
    width: 15px;
  }

  .search-tip-text {
    font-size: 12px;
    color: #666;
  }

  .search-btn {
    font-size: 12px;
    background-color: #ff6900;
    color: #FFF;
    padding: 5px 8px;
    border-radius: 100px;
  }
</style>