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-intersect-class

v1.0.8

Published

An Alpine.js plugin to easily add CSS classes to an element when it enters the viewport.

Downloads

1,203

Readme

Alpine.js Intersect-Class Plugin

An Alpine.js plugin to easily set CSS classes to an element when it enters the viewport. Especially useful for animating elements when scrolling.

<!-- A little something like this.. -->
<div x-data x-intersect-class="is-visible"></div>

Demo

Click here to take a look at the examples (View Source)


Installation

You can use this plugin by either including it from a <script> tag or installing it via NPM:

Via CDN

You can include the CDN build of this plugin as a <script> tag, just make sure to include it BEFORE Alpine's core JS file.

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

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

Via NPM

You can install Intersect from NPM for use inside your bundle like so:

npm install alpinejs-intersect-class

Then initialize it from your bundle:

import Alpine from 'alpinejs'
import intersectClass from 'alpinejs-intersect-class'

Alpine.plugin(intersectClass)

...

Usage

Use the x-intersect-class Attribute to set the CSS class that should be added to the element once it comes to the browsers viewport.

<div x-data x-intersect-class="fade-in"></div>

Make sure that you have defined that class in your CSS files, like fade-in in this example.

The .once Modifier

You can use the .once modifier if you want to trigger the CSS class only on the first appearance of an element.

<div x-data x-intersect-class.once="fade-in"></div>

.threshold, .half and .full Modifier

Control the threshold property of the IntersectionObserver. This works the same way like it does with Alpines Intersect Plugin.

<div x-data x-intersect-class.threshold.75="fade-in"></div>

FAQ

Couldn't I just use Alpines Intersect Plugin to do something like this?

<div x-data="{ shown: false }" x-intersect="shown = true" :class="{ 'is-visible': shown }">

You definitely could. And if you are using the Intersect plugin already, then you probably should! The point of the Intersect-Class plugin is to provide this functionality with as few attributes as possible so that frontend developers will actually use it.

Do I need the x-data attribute?

Yes, you do. With x-data you define a Alpine.js Component. Click here to read more about x-data in the Alpine.js documentation.

My elements are flashing when the page is loading

This is a known problem with JavaScript animations. It's because the JS takes a moment to initialize. The solution can be different depending on what kind of animation you want to run.

A good starting point could be the use of x-cloak. Initial use of the CSS class can also be helpful, like I did for the CSS animations demo.