• Stars
    star
    161
  • Rank 233,470 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sticky Directive for Angular 2+

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:

<div #spacer></div>
<div stickyThing [spacer]="spacer">
  I am sticky!
</div>

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.

<div #boundary style="height:1000px;">
  <div #spacer></div>
  <div stickyThing [spacer]="spacer" [boundary]="boundary">
    I am sticky but only inside #boundary!
  </div>
</div>

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.

<div #spacer></div>
<div stickyThing [spacer]="spacer" [enable]="enableSticky">
  I can become sticky only when enableSticky is true!
</div>

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.

<div #boundary style="height:1000px;">
  <div #spacer></div>
  <div stickyThing [spacer]="spacer" marginTop="30">
    I leave 30px of space to the top when I'm sticky!
  </div>
</div>

Event Outputs

<div #boundary style="height:1000px;">
  <div #spacer></div>
  <div stickyThing (stickyPosition)="consoleLog($event)" (stickyStatus)="consoleLog($event)" marginTop="30" marginBottom="50">
    I leave 30px of space to the top when I'm sticky!
  </div>
</div>

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.

<div class="scrollable-container">
  <p>I'm special, since my content scrolls and not the body.</p>
  <p>...</p>
  <div stickyThing [scrollContainer]="'.scrollable-container'">Sticky</div>
</div>
.scrollable-container {
  height: 300px;
  margin: 3em auto;
  overflow: scroll;
}

Scroll-Container in Action

Patron

❤️ W11K - The Web Engineers

❤️ theCodeCampus - Trainings for Angular and TypeScript

More Repositories

1

calendar-for-trello

Calendar for Trello - hosted at
TypeScript
118
star
2

ngx-componentdestroyed

Unsubscribe from Observables in Angular Components
TypeScript
97
star
3

scalamodules

ScalaModules is an elegant and intuitive domain specific language for OSGi development written in the Scala programming language.
Scala
91
star
4

slf4s

Simple Scala facade for SLF4J
Scala
82
star
5

Tydux

Type safe state management for web applications
TypeScript
36
star
6

fabs

Fabulous AngularJS Build System
JavaScript
30
star
7

bnd4sbt

BND plugin for SBT
Scala
30
star
8

w11k-angular-seo-header

22
star
9

rx-ninja

Utilities for RxJS
TypeScript
20
star
10

scala-lang-osgi

OSGified Scala libraries
Scala
20
star
11

angular-git-info

A schematic that adds the git version info to your project
TypeScript
19
star
12

w11k-select

TypeScript
18
star
13

scalamodulesexamples

Examples for ScalaModules.
Scala
10
star
14

rx-utils

Utilities for RxJS
TypeScript
10
star
15

ngx-present

Open Source, Angular Based Presentation Tool
TypeScript
9
star
16

gameoflife

Idiomatic and simple Scala implementation of the famous Game of Life.
Scala
9
star
17

demo-akka

Akka Demos
Scala
8
star
18

lsql

Java SQL Database Library
Java
8
star
19

diff-patch-sync

diff-patch-sync is a TypeScript library for syncing collaborative web-applications with REST-backends in order to make them offline-capable.
TypeScript
8
star
20

advanced-scala

Some more or less advanced examples for Scala
Scala
7
star
21

w11k-flash

JavaScript
5
star
22

Tydux-angular

TypeScript
5
star
23

fabs-boilerplate

JavaScript
5
star
24

scalatrain

Example code for German Scala book "Durchstarten mit Scala"
Scala
5
star
25

ng-dropping-stones

TypeScript
4
star
26

basecamp3-chrome

JavaScript
4
star
27

ngx-select

Single and Multi-Select Angular Component with Material and Bootstrap Templates
TypeScript
3
star
28

demo-scalaquery

Demo for ScalaQuery
Scala
3
star
29

angular-typescript-build

JavaScript
3
star
30

eclipselauncherx

Java
3
star
31

w11k-slides

CSS
3
star
32

eslint-config

ES Lint Config for W11K Projects
JavaScript
2
star
33

sbt-pinfo

Scala
2
star
34

scalatip

Collects Scala tips from Twitter
Scala
2
star
35

chatter

Twitter-like demo for Scala and Lift.
Scala
2
star
36

kix

Football World Cup tipping implemented with Lift (W-JAX Challenge 2009)
Scala
2
star
37

mvn2sbt

Creates an SBT project from a Maven POM
2
star
38

mssql-server-linux-docker-compose

docker-compose.yml which creates the mssql-server-linux docker image and automatically executes provided .sql files
Shell
2
star
39

wjax11ajaxcomet

Scala
1
star
40

webrcp

Webstart for RCP
Java
1
star
41

rcc

Rich Co-op Client
Scala
1
star
42

w11k-dropdownToggle

JavaScript
1
star
43

w11k-select-bower

1
star
44

tslint-presets

tslint preset used at W11K
1
star
45

lsql-example

Java
1
star
46

ngx-lifecycle-mixins

TypeScript
1
star
47

ts-mixins

TypeScript
1
star
48

scala-live

Scala
1
star
49

minimal.g8

Minimal g8 project using a DefaultProject defining dependencies to specs, ScalaCheck and Mockito.
Scala
1
star
50

diff-patch-sync-demo

TypeScript
1
star
51

angular7-evan-network

Connect with a Angular 7 App to the evan test network
TypeScript
1
star
52

superset-chart-plugins

TypeScript
1
star