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

ng2-sticky-nav

v10.0.0

Published

A super simple, native, Angular 2+ directive for navs (or anything) that become sticky as you scroll down.

Downloads

1,859

Readme

ng2-sticky-nav

A super simple, native, Angular 2+ directive for navs (or anything) that become sticky as you scroll down.

Installation

To install this directive in your project, run:

$ npm install ng2-sticky-nav --save

Using this directive

and then from your Angular module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// Import your library
import { StickyNavModule } from 'ng2-sticky-nav';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,

    // import module here
    StickyNavModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Once you have imported the module, you can use the directive like so:

<nav class="custom-nav-class" ngStickyNav></nav>

The div with ngStickyNav will now stick to the top once you reach its original position while scrolling down. It will also return to its original position when scrolling up.

Important Recommendations

Controlling height

Since your nav will be removed from the static DOM when it sticks, I recommend setting up your html/css to account for the lost height like so:

<!-- wrap your header in another div-->
<div class="sticky-nav-wrapper">
  <nav class="sticky-nav" ngStickyNav></nav>
</div>

Corresponding css:

:root {
  /* setting var for nav height, you can do this however you want */
  --sticky-nav-height: 40px;
}

.sticky-nav-wrapper {
  /* set min height on wrapper so that the page
     doesn't awkwardly snap and lose height */
  min-height: var(--sticky-nav-height);
}

.sticky-nav {
  height: var(--sticky-nav-height);

  /* other styles */
}

Controlling z-index

Since your nav will be floating on top of other components, be sure that you style your nav so that its z-index is greater than the components it will go over.

In an attempt to make this directive as simple and generic as possible, this is NOT done out of the box.

Optional Attributes

stickyClass

stickyClass gives you the ability to assign a css class to your navbar when sticky. The styling will automatically be removed when the navbar is not sticky. For example, you can use this to add a box shadow to your nav when sticky like so:

<!-- sample.component.html -->
<div class="sticky-nav-wrapper">
  <nav class="sticky-nav" ngStickyNav stickyClass="sticky-box-shadow"></nav>
</div>
/* sample.component.css */
.sticky-nav-wrapper {
  min-height: 40px;
}

.sticky-nav {
  height: 40px;
}

.sticky-box-shadow {
  box-shadow: 0px 0px 10px 5px rgba(0,0,0,0.5);
}

Now your nav will have a slight shadow when in sticky mode. This example can be useful for when your nav is the same color as the components it will be sitting on top of.

###stickyEnabled

stickyEnabled gives you the ability to toggle whether the navbar should stick based on external logic.

<nav class="custom-nav-class" ngStickyNav [stickyEnabled]="customStickyLogic"></nav>

Note: If the condition changes, the element will only update after scrolling past it's original position. For example, if stickyEnabled is true and the user scrolls past the element, it will become sticky. If the condition changes to false, the element will remain sticky until it has returned to its original position, and subsequent scrolling will not cause the element to stick.

/* sample.component.ts */

export class sampleComponent{

  customStickyLogic:boolean 
  
  ngDoCheck(){
   this.customStickyLogic = this.performLogicCheck();
  }
}

License

MIT © Liam Stokinger