1
我不知道Renderer2
是否有機制來避免佈局垃圾。讓我們這些搬遷提示樣本指令,如果有在屏幕上沒有更多的空間:做指令是否避免佈局垃圾?
import { Directive, ElementRef, HostListener, AfterViewInit, Renderer2 } from '@angular/core';
@Directive({
selector: '[fooDirective]',
exportAs: 'fooDirective'
})
export class FooDirective implements AfterViewInit {
constructor(private el: ElementRef, private renderer: Renderer2) {
}
public ngAfterViewInit(): void {
this.onResize();
}
@HostListener('window:resize')
public onResize(): void {
this.el.nativeElement.style.left = null;
this.el.nativeElement.style.top = null;
const bounds = this.el.nativeElement.getBoundingClientRect();
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
if (windowWidth < bounds.right) {
this.renderer.setStyle(this.el.nativeElement, 'left', `calc(100% - ${bounds.right - windowWidth}px)`);
}
if (windowHeight < bounds.bottom) {
this.renderer.setStyle(this.el.nativeElement, 'top', `calc(0px - ${bounds.bottom - windowHeight}px)`);
}
}
}
有在onResize
兩種情況可能與被overlaped例如https://github.com/wilsonpage/fastdom,但我不知道是否有必要?也許angular2處理它本身?