@codingheads/sticky-header

中文文档
1.0.2发布于 2021/07/10GPL-3TypeScriptESM + CJS零依赖近 30 天下载 5729

A library that allows you to create sticky headers. It uses position: sticky and IntersectionObserver

原始使用文档
文档目录3 节
  1. Installing
  2. Initializing in JavaScript
  3. Warnings

Sticky Header

This library allows you to create sticky headers. It uses position: sticky and IntersectionObserver.

The plugin doesn't require jQuery, but it adds itself to jQuery if jQuery exists on the page.

Installing

Use npm (or yarn) to install the package.

npm install --save @codingheads/sticky-header

Initializing in JavaScript

To initialize the library, you need to create a new instance of the StickyHeader class:

import StickyHeader from '@codingheads/sticky-header';

const header = document.querySelector('header.page-header');
new StickyHeader(header, options);

Or using jQuery:

import '@codingheads/sticky-header';

$('header.page-header).stickyHeader(options);

The options object can have the following properties:

  • mainClass - the class added when the plugin is initialized (default: sticky)
  • pinnedClass - the class added when the element becomes "stuck" (default: sticky-pinned)
  • unpinnedClass - the class added when the element becomes "unstuck" (default: sticky-unpinned)
  • offset - the offset (in pixels) where the element should become "stuck" (default: 0)
  • addBodyClasses - add the pinnedClass and unpinnedClass classes to the body element as well (default: true)

Warnings

  1. The plugin uses position: sticky. This is supported in all modern browsers. However, position: sticky has some requirements: you must not have parent elements with overflow: hidden, or otherwise it will not work (the position will be static). If you cannot get it to start, check that you don't have overflow: hidden on a parent element.

  2. If you change the header size depending on the "stuck"/"unstuck" status, you will probably need to prevent the window from scrolling when this happens (the header will push the content).

If you have a #wrapper element around the content (including the header), you could do something like this:

header.sticky {
  top: 0;
  position: sticky;
}

/** fix for when the header grows in size when it becomes unpinned and the scroll position changes (we need to scroll more) **/
@supports (position: sticky) {
  body.sticky-unpinned #wrapper {
    overflow-anchor: none;
  }
}

版本与发布记录

@codingheads/sticky-header 共发布 3 个版本,首次发布于 2021/07/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2021/07/105 年前
1.0.12021/07/105 年前
1.0.02021/07/105 年前

关于 @codingheads/sticky-header 的常见问题

@codingheads/sticky-header 是什么?

A library that allows you to create sticky headers. It uses position: sticky and IntersectionObserver

如何安装 @codingheads/sticky-header?

在项目目录运行 npm install @codingheads/sticky-header,或使用 pnpm add / yarn add / bun add。

@codingheads/sticky-header 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codingheads/sticky-header 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@codingheads/sticky-header 可以商用吗?

@codingheads/sticky-header 使用 GPL-3 许可。声明的许可为 GPL-3,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@codingheads/sticky-header 的下载量有多少?

根据 npm 官方下载统计,@codingheads/sticky-header 在 2026/10/10 – 2026/11/08 这 30 天内被下载 5,729 次,日均约 191 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codingheads/sticky-header 还在维护吗?

最新版本 1.0.2 发布于 2021/07/10(5 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/07/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。