# sticky-section

组件类型:UniStickySectionElement

吸顶布局容器

注意:暂时仅支持作为list-view的子节点, sticky-section不支持css样式!

# 兼容性

Web 微信小程序 Android iOS HarmonyOS 系统版本 HarmonyOS
4.02 x 3.98 4.11 5.0.5 4.71

# 属性

名称 类型 默认值 兼容性 描述
push-pinned-header boolean true
sticky-section元素重叠时是否继续上推
padding Array<number> [0,0,0,0]
长度为 4 的数组,按 top、right、bottom、left 顺序指定内边距

# sticky-section使用场景

  1. 父元素滚动过程中,多个元素有固定到父元素顶部的需求。

    父元素中多个元素吸顶需要使用sticky-section组件。sticky-section组件作为sticky-header组件的父容器。sticky-section组件会控制子元素的滚动吸顶业务。sticky-section组件之间可通过push-pinned-header属性控制吸顶重叠时是否上推。

    示例:

    <list-view id="list-view" style="flex: 1; background-color: #f5f5f5;">
    	<sticky-section v-for="sectionId in 3" :id="sectionId" push-pinned-header=false>
    		<sticky-header>
    			<text style="padding: 20px; background-color: #f5f5f5;">sticky-header吸顶--{{sectionId}}</text>
    		</sticky-header>
    		<list-item v-for="index in 20" :key="index" style="padding: 15px; margin: 5px 0;background-color: #fff;border-radius: 5px;">
    			<text class="text">itme-content-{{index}}</text>
    		</list-item>
    	</sticky-section>
    </list-view>
    

注意

  • sticky-section组件支持存放多个sticky-header子组件,多个sticky-header滚动吸顶时,后一个sticky-header会停靠在前一个sticky-header的末尾处, 仅限于同一个sticky-section父容器。多个sticky-section吸顶停靠通过push-pinned-header控制。
  • 安卓平台、iOS平台sticky-section组件是虚拟组件不会真实渲染。仅支持padding属性控制子元素位置。其他排版需求要交给子元素实现
  • 鸿蒙平台不支持padding属性,但是sticky-section可以设置样式
  • Android平台sticky-section组件作为list-view的子元素时需要注意,sticky-section子元素仅支持sticky-header、list-item,其他元素无法正常显示

# 子组件

支持所有组件

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<template>
  <page-head title="sticky-section"></page-head>
  <list-view id="list-view" ref="list-view" show-scrollbar=false class="page" :scroll-into-view="scrollIntoView"
    @scroll="onScroll" @scrollend="onScrollEnd" bounces="false" refresher-enabled="true" :refresher-triggered="refresherTriggered" @refresherrefresh="onRefresherrefresh">
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
      <button @click="gotoStickyHeader('C')" size="mini">跳转到id为C的sticky-header位置上</button>
    </list-item>
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
      <button @click="appendSectionItem(0)" size="mini">第一组 section 新增5条内容</button>
    </list-item>
    <list-item style="padding: 10px; margin: 5px 0;align-items: center;" :type=20>
      <button @click="deleteSection()" size="mini">删除第一组 section</button>
    </list-item>
    <sticky-section v-for="(section) in sectionArray" :key="section.name" :padding="sectionPadding"
      :push-pinned-header="true">
      <sticky-header :id="section.name">
        <text class="sticky-header-text">{{section.name}}</text>
      </sticky-header>
      <list-item v-for="(list) in section.list" :key="list.text" :name="list.text" class="content-item" :type=10>
        <text class="text">{{list.text}}</text>
      </list-item>
    </sticky-section>
    <list-item v-if="sectionArray.length > 0" style="padding: 10px; margin: 5px 0;align-items: center;" :type=30>
      <!-- <text style="color: #aaa">到底了</text> -->
      <button @click="toTop" size="mini">回到顶部</button>
    </list-item>
  </list-view>
</template>

<script>
  export type sectionData = {
    name : string,
    list : sectionListItem[]
  }
  export type sectionListItem = {
    text : string
  }
  export default {
    data() {
      return {
        data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
        sectionPadding: [0, 10, 0, 10] as Array<number>,
        scrollIntoView: "",
        scrolling: false,
        sectionArray: [] as sectionData[],
        appendId: 0,
        refresherTriggered: false,
        isReady: false//自动化测试使用
      }
    },
    onLoad() {

    },
    onReady() {
      //延迟onReady再加载数据 校验issues:14705 bug
      this.initSectionArray()
      this.isReady = true
    },
    methods: {
      initSectionArray() {
        this.sectionArray = [] as sectionData[]
        console.log("initSectionArray start", this.sectionArray.length)
        this.data.forEach(key => {
          const list = [] as sectionListItem[]
          for (let i = 1; i < 11; i++) {
            const item = { text: key + "--item--content----" + i } as sectionListItem
            list.push(item)
          }
          const data = { name: key, list: list } as sectionData
          this.sectionArray.push(data)
        }
        )
        console.log("initSectionArray end", this.sectionArray[0].name)
      },
      toTop() {
        this.scrollIntoView = ""
        uni.getElementById("list-view")!.scrollTop = 0
      },
      //用于自动化测试
      listViewScrollByY(y : number) {
        const listview = this.$refs["list-view"] as UniElement
        // listview.scrollBy(0, y)
        listview.scrollTop = y
      },
      gotoStickyHeader(id : string) {
        // #ifdef APP
        this.scrollIntoView = id
        // #endif
        // #ifdef WEB
        console.log("web端不支持该功能")
        // #endif
      },
      onScroll() {
        this.scrolling = true
      },
      onScrollEnd() {
        this.scrolling = false
        //滚动后重置scrollIntoView = ""
        if (this.scrollIntoView != "") {
          this.scrollIntoView = ""
        }
      },
      appendSectionItem(index : number) {
        const data = this.sectionArray[index]
        this.appendId++
        const list = [
          { text: data.name + "--item--content----new1--" + this.appendId } as sectionListItem,
          { text: data.name + "--item--content----new2--" + this.appendId } as sectionListItem,
          { text: data.name + "--item--content----new3--" + this.appendId } as sectionListItem,
          { text: data.name + "--item--content----new4--" + this.appendId } as sectionListItem,
          { text: data.name + "--item--content----new5--" + this.appendId } as sectionListItem
        ] as sectionListItem[]
        data.list.unshift(...list)
      },
      deleteSection() {
        this.sectionArray.shift()
      },
      onRefresherrefresh(_ : UniRefresherEvent) {
        this.refresherTriggered = true;
        setTimeout(() => {
          this.initSectionArray()
          this.refresherTriggered = false;
        }, 1000)
      },
    }
  }
</script>

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

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

  .content-item {
    padding: 15px;
    margin-bottom: 10px;
    background-color: #fff;
  }
</style>

# 参见