scroll-fold-nav2.uvue 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<template>
  <view class="page">
    <scroll-view :scroll-y="true" @scroll="onScroll" class="scroll-view">
      <view class="content">
        <view style="height: 110px;">
          <!-- 垫高专用 -->
        </view>
        <view v-for="(item,index) in 20" :key="index" style="height: 100px;background-color: #FFF;">
          content-{{item}}
        </view>
      </view>
    </scroll-view>

14 15 16 17 18 19 20 21 22 23 24 25 26
    <view ref="top-box" class="top-box" style="height:110px;background-color:rgba(255,255,255,0)">
      <view ref="scroll-fold-nav" class="scroll-fold-nav">
        <view style="margin-left: 30px;">DCloud 为开发者而生</view>
      </view>
      <view @click="back" class="back" style="position: absolute;top:35px;left: 8px;">
        <image src="/static/template/scroll-fold-nav/back.png" style="width: 20px;" mode="widthFix"></image>
      </view>
      <view ref="search" class="search" @click="toSearchPage" style="width:700rpx;top:0px">
        <view style="flex-direction: row;">
          <image src="/static/template/scroll-fold-nav/search.png" style="width: 15px;" mode="widthFix"></image>
          <text class="search-tip-text">请输入你要搜索的内容</text>
        </view>
        <text class="search-btn">搜索</text>
27 28 29 30 31 32
      </view>
    </view>

  </view>
</template>

33
<script>
34 35 36 37 38
  import ScrollEvent from 'io.dcloud.uniapp.runtime.ScrollEvent';
  export default {
    data() {
      return {
        scrollTop: 0,
39 40 41 42
        searchWidth: 700,
        searchNode: null as INode | null,
        boxNode: null as INode | null,
        navNode: null as INode | null
43 44 45 46
      }
    },
    methods: {
      onScroll(e : ScrollEvent) {
47 48 49 50 51 52 53 54 55 56
        let scrollTop = e.detail.scrollTop
        this.boxNode?.style?.setProperty('height', (110 - (scrollTop > 40 ? 40 : scrollTop)) + 'px');
        this.boxNode?.style?.setProperty('background-color', 'rgba(255, 255, 255, ' + (scrollTop * 3 > 100 ? 100 : scrollTop * 3) / 100 + ')');
        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()  // 这么写用不了
57
        // 这么写可以用
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        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'
        });
80 81 82
      }
    },
    onLoad() {
83 84 85 86 87 88
    },
    onReady() {
      this.searchNode = this.$refs['search'] as INode;
      this.boxNode = this.$refs['top-box'] as INode;
      this.navNode = this.$refs['scroll-fold-nav'] as INode;
    }
89 90 91 92
  }
</script>

<style>
93
  .page {
94 95 96 97 98
    flex: 1;
    background-color: #fbdf0d;
  }

  .scroll-view {
99
    flex: 1;
100 101 102 103 104 105
  }

  .top-box {
    position: fixed;
    top: 0;
    padding-top: 25px;
106 107
    align-items: flex-end;
    border-bottom: 1px solid #efefef;
108 109 110 111
  }

  .scroll-fold-nav {
    height: 44px;
112 113
    width: 750rpx;
    justify-content: center;
114 115 116 117 118 119 120
  }

  .search {
    background-color: #FFFFFF;
    border: 1px solid #fbdf0d;
    height: 35px;
    border-radius: 100px;
121 122 123 124
    margin: 0 25rpx;
    padding: 8px;
    flex-direction: row;
    align-items: center;
125
    justify-content: space-between;
126 127 128 129 130 131 132 133 134 135 136 137 138
  }

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

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