Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position: sticky polyfill for angular 2

Tags:

Are there any polyfill available for angular 2 position:sticky. I have been able to find a few, but most of them are jquery based, there seem to be no implementation available for angular 2:

https://github.com/wilddeer/stickyfill

I need to know the sample usage with angular 2,

for Javascript it is as follows:

var stickyElements = document.getElementsByClassName('sticky');

for (var i = stickyElements.length - 1; i >= 0; i--) {
    Stickyfill.add(stickyElements[i]);
}
like image 790
Sahil Agarwal Avatar asked Apr 10 '17 11:04

Sahil Agarwal


People also ask

Why position sticky is not working?

Sticky Element Has Parent(s) with overflow PropertyIf the sticky element has a parent or ancestor with overflow: hidden , overflow: auto , or overflow: scroll , then position: sticky will not work properly.

What is Stickyfill?

Stickyfill is a polyfill. This means that it implements a feature (sticky positioning in this case) that already exists in some browsers natively, and allows to use this feature in the browsers that don't support it yet and older versions of the browsers that didn't support it at the time. This is its only purpose.


1 Answers

I added it as a directive on angular 1.4 and npm package "stickyfilljs":"^2.0.3"

import { Directive, ElementRef } from '@angular/core';
import * as Stickyfill from 'stickyfilljs';


@Directive({
  selector: '[StickyPolyfill]'
})
export class StickyPolyfillDirective {

  constructor(private elementRef: ElementRef) { }

  ngAfterViewInit() {
    Stickyfill.addOne(this.elementRef.nativeElement);
  }
}

And in the component I have:

<div class="top-bar-container stickyMenu" StickyPolyfill>

Don't forget to include sticky in CSS:

  .stickyMenu {
    position: -webkit-sticky;
    position: -moz-sticky;
    position: -o-sticky;
    position: -ms-sticky;
    position: sticky;
    top: 0px;       
  }
like image 151
Igor Jovanoski Avatar answered Sep 24 '22 10:09

Igor Jovanoski