sticky-section.uvue 2.6 KB
Newer Older
雪洛's avatar
雪洛 已提交
1
<template>
shutao-dc's avatar
shutao-dc 已提交
2
  <page-head title="sticky-section"></page-head>
3
  <button style="position: fixed; width: 50px; height: 50px;" @click="listViewScrollByY(1000)">测试</button>
雪洛's avatar
雪洛 已提交
4
  <list-view id="list-view" ref="list-view" show-scrollbar=false class="page" :scroll-into-view="scrollIntoView"
5
  @scroll="onScroll" @scrollend="onScrollEnd" rebound="false">
雪洛's avatar
雪洛 已提交
6 7
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type = 20>
      <button @click="gotoStickyHeader('C')" size="mini">跳转到id为C的sticky-header位置上</button>
8
    </list-item>
W
wanganxp 已提交
9
    <sticky-section v-for="(sectionText) in data" :padding="sectionPadding" :push-pinned-header="true">
shutao-dc's avatar
shutao-dc 已提交
10
      <sticky-header :header-id="sectionText" :id="sectionText">
W
wanganxp 已提交
11 12 13 14 15 16
        <text class="sticky-header-text">{{sectionText}}</text>
      </sticky-header>
      <list-item v-for="i in 10" class="content-item" :type=10>
        <text class="text">{{sectionText}}--item--content----{{i}}</text>
      </list-item>
    </sticky-section>
雪洛's avatar
雪洛 已提交
17
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type= 30>
W
wanganxp 已提交
18 19 20 21 22 23 24 25 26 27 28
      <!-- <text style="color: #aaa">到底了</text> -->
      <button @click="toTop" size="mini">回到顶部</button>
    </list-item>
  </list-view>
</template>

<script>
  export default {
    data() {
      return {
        data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
雪洛's avatar
雪洛 已提交
29 30
        sectionPadding: [0, 10, 0, 10] as Array<number>,
        scrollIntoView: "",
31
        scrolling: false
W
wanganxp 已提交
32 33
      }
    },
雪洛's avatar
雪洛 已提交
34 35 36 37
    methods: {
      toTop(){
        this.scrollIntoView = ""
        uni.getElementById("list-view")!.scrollTop = 0
W
wanganxp 已提交
38 39 40
      },
      //用于自动化测试
      listViewScrollByY(y : number) {
DCloud-yyl's avatar
DCloud-yyl 已提交
41
        const listview = this.$refs["list-view"] as UniElement
shutao-dc's avatar
shutao-dc 已提交
42 43
        // listview.scrollBy(0, y)
        listview.scrollTop = y
雪洛's avatar
雪洛 已提交
44
      },
45 46 47 48 49 50 51
      gotoStickyHeader(id : string) {
        // #ifdef APP
        this.scrollIntoView = id
        // #endif
        // #ifdef WEB
        console.log("web端不支持该功能")
        // #endif
雪洛's avatar
雪洛 已提交
52 53 54 55 56 57 58 59 60
      },
      onScroll() {
        this.scrolling = true
      },
      onScrollEnd() {
        this.scrolling = false
        //滚动后重置scrollIntoView = ""
        if(this.scrollIntoView != "") {
          this.scrollIntoView = ""
shutao-dc's avatar
shutao-dc 已提交
61
        }
W
wanganxp 已提交
62 63 64 65 66 67
      }
    }
  }
</script>

<style>
shutao-dc's avatar
shutao-dc 已提交
68
  .page {
W
wanganxp 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
    flex: 1;
    background-color: #f5f5f5;
  }

  .sticky-header-text {
    font-size: 16px;
    padding: 8px;
    color: #959595;
    background-color: #f5f5f5;
  }

  .content-item {
    padding: 15px;
82
    margin-bottom: 10px;
W
wanganxp 已提交
83 84
    background-color: #fff;
  }
雪洛's avatar
雪洛 已提交
85
</style>