3.30.1 • Published 3 years ago

@zhinan-oppo/sticky v3.30.1

Weekly downloads
34
License
MIT
Repository
github
Last release
3 years ago

sticky

Install

使用 yarn 安装

yarn add @zhinan-oppo/sticky

引用方式

import { initStickyElement, initAllBySelector } from '@zhinan-oppo/sticky';

接口

  • initStickyElement(element, options)
    • 参数element是需要被设置为黏性布局的 HTMLElement
    • 参数options是可选的object,包含以下属性
      • container: 可选,默认为element.offsetParent
        • 如果container未指定且element.offsetParentnull,会报错,具体参见:offsetParent
      • scrollHandlers: 可选,传递给scrollHandle的回调函数,具体参见:scrollHandle
  • initAllBySelector(selector, root)
    • 参数selector是可选的字符串,传递给root.querySelectorAll,所有被选中的 element 会逐一传入到initStickyElement中以默认参数初始化
    • 参数root是可选的 DOM 节点,默认为window.document

Instruction (Examples)

sticky 布局

粘性布局的 JS 实现方法。sticky-container 为布局容器,sticky-item 为具体的粘性元素。

向下滚动时:

  • 当 sticky-container 的上边界触碰到屏幕顶部,sticky-item 变为 fixed,表现为粘在屏幕上
  • 当 sticky-item 的底部和 sticky-container 的底部贴在一起时,sticky-item 变为 absolute,随 sticky-container 正常滚走。

  • sticky 布局介绍

initStickyItem:手动初始化.sticky-item

<div class="sticky-container" style="position: relative">
  <div class="sticky-item" id="sticky">
  </div>
</div>
const element = document.getElementById('sticky');
initStickyItem(element, {
  // scrollHandlers 实际上是监听在 container 上的
  scrollHandlers = {
    always: (dom, distance, total) => {},
  },
});
initStickyItem(element, {
  container = element.offsetParent,
  scrollHandlers = {
    always: (dom, distance, total) => {},
  },
});

initAllBySelector: 将所有 class 列表中带sticky-item的元素都设置为黏性布局

<div class="sticky-container" style="position: relative">
  <div class="sticky-item">
  </div>
</div>
import { initAllBySelector } from '@zhinan-oppo/sticky';
document.addEventListener('DOMContentLoaded', event => {
  initAllBySelector('.sticky-item');
});
3.30.1

3 years ago

3.30.0

3 years ago

3.29.1

3 years ago

3.28.0

3 years ago

3.28.1

3 years ago

3.25.0

3 years ago

3.24.0

3 years ago

3.22.0

3 years ago

3.18.3

4 years ago

3.16.3

4 years ago

3.16.2

4 years ago

3.15.1

4 years ago

3.11.3

4 years ago

3.11.2

4 years ago

3.11.1

4 years ago

3.10.4

4 years ago

3.10.3

4 years ago

3.9.5

4 years ago

3.9.4

4 years ago

3.9.3

4 years ago

3.9.2

4 years ago

3.9.1

4 years ago

3.9.0

4 years ago

3.8.0

4 years ago

3.7.1

4 years ago

3.7.0

4 years ago

3.4.1

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.4.0

4 years ago

1.0.14

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.0

4 years ago