2.0.0 • Published 8 months ago

@chayson/stick-it v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

StickIt

StickIt is a modern, customizable sticky header plugin that allows you to "stick" elements to the top of the viewport on demand. It is a modernized and enhanced version of the original Headhesive.js by Mark Goodyear, updated and maintained by Chayson Media Group.

StickIt supports configurable offsets, numeric or element-based triggers, and includes modern development features like source maps and modular code.


Table of Contents

  1. Features
  2. Installation
  3. Usage
  4. Configuration
  5. Examples
  6. Development
  7. Credits
  8. License

Features

  • On-Demand Sticky Headers: Stick any element to the top of the page when you scroll past a defined threshold.
  • Configurable Offsets: Trigger sticky behavior using either:
    • A numeric offset (in pixels).
    • An existing element on the page.
  • Modular Codebase: Written in ES6 modules for modern JavaScript development.
  • Production Ready: Minified builds (stick-it.min.js) with source maps for debugging.
  • Customizable: Fully customizable CSS classes and lifecycle callbacks.
  • Lightweight: No external dependencies.

Installation

You can install StickIt via NPM:

npm install @chayson/stick-it

Alternatively, include the pre-built UMD version via a <script> tag:

<script src="dist/stick-it.min.js"></script>

Usage

Including StickIt in Your Project

1. Using ES Modules (Recommended)

If you are using a bundler like Rollup, Webpack, or Vite, you can import StickIt as an ES module:

import StickIt from './dist/stick-it.min.js';

2. Using UMD Build

Include StickIt in your project using a <script> tag. This makes StickIt available globally.

<script src="dist/stick-it.min.js"></script>
<script>
  const stickyHeader = new StickIt('.my-header', {
    offset: 200,
    onStick: () => console.log('Header stuck!'),
    onUnstick: () => console.log('Header unstuck!')
  });
</script>

Configuration

StickIt accepts an object with customizable options:

OptionTypeDefaultDescription
offsetnumber / string300Numeric scroll offset (in px) or a CSS selector for an element.
offsetSidestring'top'Determines which side ('top' or 'bottom') is used for the offset.
classesobject{}Custom CSS classes for the sticky element. See below for class options.
throttlenumber250Delay in ms for throttling scroll and resize events.
onInitfunction() => {}Callback when StickIt is initialized.
onStickfunction() => {}Callback when the element becomes sticky.
onUnstickfunction() => {}Callback when the element is unstuck.
onDestroyfunction() => {}Callback when StickIt is destroyed.

Default Classes

StickIt applies the following CSS classes:

ClassDescription
cloneApplied to the cloned sticky element.
stickApplied when the element becomes sticky.
unstickApplied when the element is no longer sticky.

Example: Basic Usage

<!DOCTYPE html>
<html lang="en">
<head>
  <title>StickIt Demo</title>
  <script src="dist/stick-it.min.js"></script>
</head>
<body>
  <header class="my-header">My Sticky Header</header>
  <div style="height: 1500px;">Scroll down to see it stick!</div>

  <script>
    const stickyHeader = new StickIt('.my-header', {
      offset: 100,
      onStick: () => console.log('Header is now sticky!'),
      onUnstick: () => console.log('Header is unstuck.')
    });
  </script>
</body>
</html>

Development

Clone the repository and install dependencies:

git clone https://github.com/chayson/stick-it.git
cd stick-it
npm install

Build the Project

To bundle the project with Rollup, run:

npm run build

The output files will be available in the dist/ folder:

  • stick-it.js - Unminified UMD build.
  • stick-it.min.js - Minified UMD build with source maps.

Credits


License

This project is licensed under the MIT License. See the LICENSE file for details.

2.0.0

8 months ago

1.1.0

11 months ago

1.0.0

11 months ago