Vant Sticky 粘性布局

2022-05-31 13:38 更新

介紹

Sticky 組件與 CSS 中position: sticky屬性實(shí)現(xiàn)的效果一致,當(dāng)組件在屏幕范圍內(nèi)時(shí),會(huì)按照正常的布局排列,當(dāng)組件滾出屏幕范圍時(shí),始終會(huì)固定在屏幕頂部。

引入

import Vue from 'vue';
import { Sticky } from 'vant';

Vue.use(Sticky);

代碼演示

基礎(chǔ)用法

將內(nèi)容包裹在Sticky組件內(nèi)即可

<van-sticky>
  <van-button type="primary">基礎(chǔ)用法</van-button>
</van-sticky>

吸頂距離

通過(guò)offset-top屬性可以設(shè)置組件在吸頂時(shí)與頂部的距離

<van-sticky :offset-top="50">
  <van-button type="info">吸頂距離</van-button>
</van-sticky>

指定容器

通過(guò)container屬性可以指定組件的容器,頁(yè)面滾動(dòng)時(shí),組件會(huì)始終保持在容器范圍內(nèi),當(dāng)組件即將超出容器底部時(shí),會(huì)固定在容器的底部

<div ref="container" style="height: 150px;">
  <van-sticky :container="container">
    <van-button type="warning">指定容器</van-button>
  </van-sticky>
</div>
export default {
  data() {
    return {
      container: null
    };
  },
  mounted() {
    this.container = this.$refs.container;
  }
};

API

Props

參數(shù)說(shuō)明類型默認(rèn)值
offset-top吸頂時(shí)與頂部的距離,單位pxnumber | string0
z-index吸頂時(shí)的 z-indexnumber | string99
container容器對(duì)應(yīng)的 HTML 節(jié)點(diǎn)Element-

Events

事件名說(shuō)明回調(diào)參數(shù)
scroll滾動(dòng)時(shí)觸發(fā){ scrollTop: 距離頂部位置, isFixed: 是否吸頂 }


實(shí)例演示

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)