npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zhinan-oppo/sticky

v3.30.1

Published

A library to handle sticky position

Downloads

14

Readme

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');
});