Code Monkey home page Code Monkey logo

akash-gupt / ngx-headroom Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 1.77 MB

NgxHeadroom is a lightweight, high-performance widget (with no dependencies) that allows you to react to the user's scroll. The header on this site is a living example, it slides out of view when scrolling down and slides back in when scrolling up.ng example, it slides out of view when scrolling down and slides back in when scrolling up.

JavaScript 11.43% TypeScript 49.26% CSS 33.49% HTML 5.82%

ngx-headroom's Introduction

NgxHeadroom

NgxHeadroom is a lightweight, high-performance widget (with no dependencies) that allows you to react to the user's scroll. The header on this site is a living example, it slides out of view when scrolling down and slides back in when scrolling up.

Playground

To check and play with NgxHeadroom

EDITOR URL ==> https://stackblitz.com/edit/ngx-headroom

APP URL => https://ngx-headroom.stackblitz.io

To play More ==> https://wicky.nillia.ms/headroom.js/playroom/

Why use it?

Fixed headers are a popular approach for keeping the primary navigation in close proximity to the user. This can reduce the effort required for a user to quickly navigate a site, but they are not without problems…

Large screens are usually landscape-oriented, meaning less vertical than horizontal space. A fixed header can therefore occupy a significant portion of the content area. Small screens are typically used in a portrait orientation. Whilst this results in more vertical space, because of the overall height of the screen a meaningfully-sized header can still be quite imposing.

Headroom.js allows you to bring elements into view when appropriate, and give focus to your content the rest of the time.

Prerequisites

NgxHeadroom will work in angular projects.

Requirements

Your div must be fixed somewhere to use NgxHeadroom.

.header--fixed {
  position: fixed;
}

OR

.header--fixed {
  position: sticky;
}

NgxHeadroom will work in angular projects.

Table of Contents

Installation

$ npm i headroom.js --save
$ npm install ngx-headroom --save

Import style

To use css import it.

@import 'node_modules/ngx-headroom/headroom.css';

OR

Include it into angular.json .

"node_modules/ngx-headroom/headroom.css"

it will look like

 "styles": [
               ....,
              "node_modules/ngx-headroom/headroom.css"
            ],

Import modules

To work on form it is require to import NgxHeadroomModule which allow to bring elements into view when appropriate, and give focus to your content the rest of the time. All modules register in the NgModule decorator imports property.

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

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

import { NgxHeadroomModule } from 'ngx-headroom'; // <-- #1 import module

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

@NgModule({
  declarations:[AppComponent],
  imports:[ BrowserModule,
	NgxHeadroomModule
	]
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

NgxHeadroom Options

NgxHeadroom can also accept an options object to alter the way it behaves. The full structure of an options object is as follows:

Options

import { NgxHeadroomOption } from 'ngx-headroom';

let options: NgxHeadroomOption = {
  // vertical offset in px before element is first unpinned
  offset: 200,
  // scroll tolerance in px before state changes
  tolerance: 0,
  // or you can specify tolerance individually for up/down scroll
  tolerance: {
    up: 5,
    down: 0
  },
  // css classes to apply
  classes: {
    // when element is initialised
    initial: 'headroom',
    // when scrolling up
    pinned: 'headroom--pinned',
    // when scrolling down
    unpinned: 'headroom--unpinned',
    // when above offset
    top: 'headroom--top',
    // when below offset
    notTop: 'headroom--not-top',
    // when at bottom of scoll area
    bottom: 'headroom--bottom',
    // when not at bottom of scroll area
    notBottom: 'headroom--not-bottom',
    // when frozen method has been called
    frozen: 'headroom--frozen'
  }
};

app.component.html

Use Ngxheadroom selector to select the element.

<div Ngxheadroom></div>

Events

  // callback when pinned
  @Output() pinned = new EventEmitter();
  // callback when unpinned
  @Output() unpinned = new EventEmitter();
  // callback when above offset
  @Output() toped = new EventEmitter();
  // callback when  below offset
  @Output() notToped = new EventEmitter();
  // callback when at bottom of page
  @Output() bottomed = new EventEmitter();
  // callback moving away from bottom of page
  @Output() notBottomed = new EventEmitter();

Methods

The following methods are available to be called on a headroom instance:

  • destroy(): destroy the headroom instance, removing event listeners and any classes added
  • pin(): forcibly set the headroom instance's state to pinned
  • unpin(): forcibly set the headroom instance's state to unpinned
  • freeze(): freeze the headroom instance's state (pinned or unpinned), and no longer respond to scroll events.
  • unfreeze(): resume responding to scroll events

ngx-headroom's People

Contributors

akash-gupt avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.