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

alpinejs-sticky

v1.1.4

Published

Add classes when an element is stuck in the viewport

Downloads

1,409

Readme

Alpine JS Sticky

Toggle CSS class names when an element becomes sticky to the DOM 🐙

Install

With a CDN

<script
  defer
  src="https://unpkg.com/alpinejs-sticky@latest/dist/sticky.min.js"
></script>

<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

With a Package Manager

npm i -D alpinejs-sticky

yarn add -D alpinejs-sticky
import Alpine from 'alpinejs'
import sticky from 'alpinejs-sticky'

Alpine.plugin(sticky)

Alpine.start()

Example

<div x-data>
  <div class="sticky top-0 sticky-root" x-sticky="!bg-red-500">
    <p class="hidden" x-sticky.sticky-root="!block">Hello World!</p>
  </div>

  <p class="hidden" x-sticky.sticky-root="!block">Hello World!</p>
</div>

x-sticky="!bg-red-500"

This will add !bg-red-500 to the element when it is sticky to the DOM.

x-sticky.sticky-root="!block"

This will add !block to the element when the sticky-root element is sticky to the DOM.

At times you might need to apply the ! important modifier to your CSS.

This example uses Tailwind CSS classes, but any CSS class names will work.

Modifiers

As shown you can pass a class name as a modifier to target a specific HTML element.

You can also pass wait which will use the scroll event listener instead of the IntersectionObserver. Why would you want this? Well, it comes in handy when the element is already at the top of the DOM and you don't want the stuck classes applying.

Options

x-sticky-inactive

This allows you to pass classes that apply to the element when it's not stuck. These will be toggled alongside the active classes.

Using this means you do not need an ! important modifier with your CSS.

x-sticky-root

By default this is -1px 0px 0px 0px.

MDN IntersectionObserver API.

I have removed the x-sticky-threshold option as I found it unnecessary through my uses.

Stats