代码之家  ›  专栏  ›  技术社区  ›  r3plica

加载视频并播放

  •  0
  • r3plica  · 技术社区  · 5 年前

    我创建了一个视频并将其作为背景视频添加到我的网站,但我甚至不想将其加载到移动设备上,所以我这样做了:

    <video #video autoplay muted loop id="video" *ngIf="currentWidth && currentWidth >= 992">
        <source [src]="section.backgroundVideo" type="video/mp4">
    </video>
    

    在我的代码里我做到了:

    @HostListener('window:resize', ['$event'])
    onResize(event: any) {
        this.currentWidth = event.target.innerWidth;
    }
    
    public currentWidth: number;
    
    constructor(private backgroundImageService: BackgroundImageService) {}
    
    ngOnInit(): void {
        this.currentWidth = window.innerWidth;
    }
    

    @ViewChild('video', { static: true }) videoplayer: ElementRef;
    
    ngDoCheck(): void {
        if (!this.videoplayer) return;
        this.videoplayer.nativeElement.play();
    }
    

    但它一直找不到录像机。我怀疑是因为即使 当前宽度 大于992px。 有人知道我怎样才能达到我想要的效果吗?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Andrei    5 年前

    应该是的 静态:错误

    @ViewChild('video', { static: false }) videoplayer: ElementRef;
    

    因为元素在ngIf下,不能静态请求

    推荐文章