Skip to content

Affix 固钉 ​

滚动到阈值后把元素固定在视口某个位置,并保留占位元素避免布局跳动。

基础用法 ​

向下滚动时元素吸附到距顶部 20px 的位置。

vue
<template>
  <KbAffix :offset-top="20">
    <button>固定在这里</button>
  </KbAffix>
</template>
向下滚动 →
固定在容器顶部

固定在底部 ​

传 offset-bottom 后改为「元素底部越过视口底线时吸附」。

vue
<template>
  <KbAffix :offset-bottom="20">
    <button>固定在底部</button>
  </KbAffix>
</template>

限制在容器内 ​

target 传边界容器的选择器,滚动出容器后自动解除固定。

vue
<template>
  <div id="affix-scope">
    <KbAffix :offset-top="10" target="#affix-scope">
      <button>只在容器内固定</button>
    </KbAffix>
  </div>
</template>

API ​

Props ​

属性类型默认值说明
offsetTopnumber0距视口顶部固定偏移,与 offsetBottom 二选一,都传时以它为准
offsetBottomnumber—距视口底部固定偏移
targetstring—边界容器选择器,固定不会超出该容器
placeholderbooleantrue是否保留占位元素

事件 ​

事件参数说明
changeboolean固定状态变化