Created
August 5, 2016 03:51
-
-
Save guiseek/d510bc65a4a578378e90f8509fa52c8c to your computer and use it in GitHub Desktop.
Diretiva para fixar elemento visível na tela (usa positioin sticky, não tem suporte ao Chrome)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class FixedOnScroll { | |
constructor($window) { | |
this.restrict = 'A' | |
this.window = $window | |
} | |
link(scope, elem, attrs) { | |
let e = elem[0], | |
offset = attrs.offset || 0, | |
top = attrs.top || 0 | |
e.style.position = 'static' | |
e.style.top = 'auto' | |
e.style.right = 'auto' | |
e.style.bottom = 'auto' | |
e.style.left = 'auto' | |
angular.element(this.window).on('scroll', ev => { | |
if (ev.pageY > offset) { | |
e.style.position = 'sticky' | |
e.style.top = top + 'px' | |
} | |
else if (ev.pageY < offset) { | |
e.style.position = 'static' | |
e.style.top = 'auto' | |
} | |
}) | |
} | |
static directiveFactory($window) { | |
FixedOnScroll.instance = new FixedOnScroll($window) | |
return FixedOnScroll.instance | |
} | |
} | |
FixedOnScroll.directiveFactory.$inject = ['$window'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment