Guest User

Untitled

a guest
Nov 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. /*
  2. * FROM:
  3. */
  4.  
  5.  
  6. @Component({
  7. selector: 'some-other-component'
  8. })
  9. export class SomeOtherComponent {
  10. private _el: any;
  11. constructor(private _elementRef: ElementRef, private _renderer: Renderer2){}
  12.  
  13. public ngOnInit() {
  14. this._el = this._renderer.selectRootElement(this._elementRef);
  15. this.renderLoop_();
  16. }
  17.  
  18. private renderLoop_() {
  19. this.renderScrollBar_();
  20. fd.measure(() => this.renderLoop_());
  21. }
  22.  
  23. private renderScrollBar() {
  24. fd.measure(() => {
  25. const scroll = window.scrollY + 100;
  26. fd.mutate(() => {
  27. this._el.top = `${scroll}px`;
  28. });
  29. });
  30. }
  31. }
  32.  
  33. /*
  34. * TO:
  35. */
  36.  
  37. export class SomeOtherComponent {
  38. private _el: any;
  39.  
  40. @Mutate() // maybe @HostBinding is naturally a Mutation?
  41. @HostBinding('style.top') top = this._calcWindow();
  42. constructor(){}
  43.  
  44. public ngOnInit() {
  45. this._renderScrollBar();
  46. }
  47.  
  48. @Measure()
  49. private _calcWindow() {
  50. return `${window.scrollTop + 100}px` ;
  51. }
  52.  
  53. @Measure('loop')
  54. private _renderScrollBar() {
  55. this._setElTop();
  56. }
  57. }
  58.  
  59. /*
  60. * TO throttled by scroll:
  61. */
  62.  
  63. export class SomeOtherComponent {
  64. private _el: any;
  65. @ViewChild(CdkScrollable) child: cdkScrollable;
  66. @Mutate() // maybe @HostBinding is naturally a Mutation?
  67. @HostBinding('style.top') top = this._calcWindow();
  68. constructor(){}
  69.  
  70. public ngOnInit() {
  71. this._renderScrollBar();
  72. }
  73.  
  74. @Measure()
  75. private _calcWindow(event) {
  76. return `${event.target.scrollTop + 100}px` ;
  77. }
  78.  
  79. /**
  80. * Measure decorater could take 3 parameters: optional loop, throttleBy Observable, A return of the throttler
  81. * @param {string} loop Will recusively loop the method decorated.
  82. * @param {Observable<Event>=} cdkScrollable.elementScrolled() will will cancel and start the requestAnimationFrame Loop based on Obserable.
  83. * @param {Array<Event>=} A return of the throttled method.
  84. */
  85. @Measure('loop', cdkScrollable.elementScrolled(), [$event])
  86. private _renderScrollBar() {
  87. this._setElTop($event);
  88. }
  89. }
Add Comment
Please, Sign In to add comment