angular-sticky-things

by w11k

Sticky Directive for Angular 2+

147 Stars 21 Forks Last release: Not found MIT License 337 Commits 30 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

npm version

Angular Sticky Things

An Angular directive for making things sticky when the user scrolls (for Angular 2+) with no jQuery Dependency.

See the demo here.

Requirements

  • Angular (requires Angular 4.x or higher)
  • Supports all major browsers and IE11 and up (lower versions might not be supported)

Features:

  • Stick all the things!
  • Super smooth!
  • Tested in real world projects
  • Support for Angular Universal
  • Prevents page-jumping when switching to sticky mode
  • No jQuery or other dependencies - pure Angular solution

Scroll in Action

Installation

with npm:

npm install @w11k/angular-sticky-things

with yarn:

yarn add @w11k/angular-sticky-things

Now import the AngularStickyThingsModule in the corresponding Module ``` import {AngularStickyThingsModule} from '@w11k/angular-sticky-things';

@NgModule({ declarations: [ ], imports: [ AngularStickyThingsModule, ], providers: [], }) export class SomeModule { } ```

Usage:

I am sticky!

Boundary Elements

If a boundary element is defined, the sticky element scrolls only within the height of the boundary element and then stops. This is useful if you have multiple sticky elements since it prevents stacking. You can take a look at the examples.

html
I am sticky but only inside #boundary!

Hint: The boundary feature is still in beta - position errors might occur!

Spacer

The spacer is not required but prevents a page jump when the sticky effect steps in.

Enable

An

enable
(default
true
) input can be used to dynamically activate or deactivate the sticky directive (e.g. to have a sticky navbar only in certain conditions). You can take a look at the examples.
I can become sticky only when enableSticky is true!

Margins

A

marginTop
(default
0
) input can be used to add some top spacing to the sticky element, in case you don't want it to stick right at the top. It expects the
number
of pixels you want to use for the space. You can take a look at the examples. Accordingly,
marginBottom
is available.
I leave 30px of space to the top when I'm sticky!

Event Outputs

I leave 30px of space to the top when I'm sticky!

Example Output:

[Log] stickyPositon - {offsetY: 786, bottomBoundary: 1406.9999389648438, upperScreenEdgeAt: 75, marginBottom: "50", marginTop: "30"}
[Log] stickyStatus - {isSticky: false, reachedUpperEdge: true, reachedLowerEdge: false}

Scroll in Container

Per default Sticky Things expects your body to be the element that scrolls. However, if Sticky Things is used in an

overflow
-container, that container must be made known to the directive.

This is best done with a query selector. If a string is provided it will be called with

document.querySelector
. Instead an HTML element (nativeElement) can be provided as well.

Note: In a scrollable container boundary, spacer and margins don't work.

I'm special, since my content scrolls and not the body.

...

Sticky
.scrollable-container {
  height: 300px;
  margin: 3em auto;
  overflow: scroll;
}

Scroll-Container in Action

Patron

❤️ W11K - The Web Engineers

❤️ theCodeCampus - Trainings for Angular and TypeScript

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.