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

vue-affix

v0.5.2

Published

A Vue.js plugin that affixes an element in the window while you scroll, similar to Bootstrap Affix but much simpler

Downloads

16,693

Readme

Affixes an element on the screen based on a relative element

Unlike other plugins, this Vue 2 component will make it easy to affix any element while scrolling.

  • Affixes an element based on a relative element
  • Only needs 3 super simple configuration steps
  • Dispatches events for affix tracking
  • No dependencies

Make sure to check the demo to see how it works!

Installation

First install it using:

npm install --save vue-affix

or

yarn add vue-affix

Then you can either use it as a plugin:

import Affix from 'vue-affix';
Vue.use(Affix);

or you can use it as a component (note the curly braces):

import { Affix } from 'vue-affix';

export default {
  components: {
    Affix,
  },
};

Or if you wish to include it in a script tag, just include the vue-affix.min.js file located in the dist folder as so:

<script src="dist/vue-affix.min.js"></script>

Usage

  • Wrap the element in an <affix> tag
  • Set an element selector in the relative-element-selector property
  • Set a width value for the .vue-affix class in your CSS (that is the element which the affix will be applied to)

Example below:

<affix class="sidebar-menu" relative-element-selector="#example-content" style="width: 300px">
  <a href="#markup-1">Markup 1</a>
  <a href="#markup-2">Markup 2</a>
  <a href="#markup-3">Markup 3</a>
</affix>
<section id="example-content">
  <p>This is the #example-content section which the sidebar will be relatively affixed!</p>
</section>

This will make the .sidebar-menu element stay fixed while in the #example-content element viewport. Simple as that. Just don't forget to set the width to .vue-affix class! It will probably cause unexpected behavior if you don't set it!

How the plugin works

It works similar to Bootstrap's Affix plugin, it will add 3 classes (.affix-top, .affix and .affix-bottom) to the affixed element while you scroll the page.

Unlike Bootstrap's Affix, all you need to do in vue-affix is set a width value for the class .vue-affix (that's because when the position: fixed property is applied, it will lose its relative width value) and add an element in the relative-element-selector property.

vue-affix will calculate when to start/stop affixing relatively to the element you provide in the relative-element-selector property, that means you don't have to do any calculation, just set the relative element and that will be all done automatically.

You can also set an offset object with the top and bottom values that will be used as a threshold to start/stop affixing the element, but that is optional.

Configuration

The only configuration you need to do in CSS is setting a width for the .vue-affix class, the rest will be applied through props as in the example below:

<affix class="sidebar-menu" relative-element-selector="#example-content" :offset="{ top: 40, bottom: 40 }">
  <a href="#markup-1">Markup 1</a>
  <a href="#markup-2">Markup 2</a>
  <a href="#markup-3">Markup 3</a>
</affix>

Props

These are all the props you can pass to the component:

/**
 * The relative element selector string. The relative element is
 * the element you want your affix to be related to, as it will
 * not be related to the window. The element will be affixed when
 * the window reaches the relative element.
 *
 * @example '#contact'
 * @type {String}
 */
relativeElementSelector: {
  type: String,
  required: true
},

/**
 * This is the offset margin between the top/bottom of the window
 * before the affix is applied.
 *
 * @type {Object}
 */
offset: {
  type: Object,
  default: () => {
    return {
      top: 40,
      bottom: 40
    }
  }
},

/**
 * Checks if the plugin should be enabled/disabled based
 * on true/false, good for mobile when you need to disable it.
 *
 * @type {Boolean}
 */
enabled: {
  type: Boolean,
  default: true
},

/**
 * Sets if the affix should be 'scrollable' when it is
 * taller than the viewport or if it should always be
 * affixed to the top until it reaches the end of the
 * relative element. Check the demo to understand better.
 *
 * @type {Boolean}
 */
scrollAffix: {
  type: Boolean,
  default: false
},

/**
 * Sets the scrollable container to use in scroll position
 * calculations. If not set, the window object will be
 * used by default.
 *
 * @type {Object}
 */
scrollContainerSelector: {
  type: String,
  default: null,
}

Events

vue-affix will dispatch 3 different events if scroll-affix prop is set to false:

  • affixtop will be dispatched when the .affix-top class is applied, that is when you scroll above the relative element.
  • affix will be dispatched when the .affix class is applied, that is while you scroll inside the relative element.
  • affixbottom will be dispatched when the .affix-bottom class is applied, that is when you scroll below the relative element.

If scroll-affix prop is set to true, it will fire 5 different events:

  • scrollaffixscrolling will be dispatched when the affixed element is being scrolled (not fixed).
  • scrollaffixup will be dispatched when the affixed element gets fixed to the top of the screen.
  • scrollaffixdown will be dispatched when the affixed element gets fixed to the bottom of the screen.
  • scrollaffixtop will be dispatched when the affixed element reaches the top of the relative element.
  • scrollaffixbottom will be dispatched when the affixed element reaches the bottom of the relative element.

Those can be catched as I show in the example below:

<affix class="sidebar-menu" relative-element-selector="#example-content" v-on:affixbottom="yourFunction()">
  <a href="#markup-1">Markup 1</a>
  <a href="#markup-2">Markup 2</a>
  <a href="#markup-3">Markup 3</a>
</affix>