CSS: Position loading indicator in the center of the screen

后端 未结 7 1237
无人及你
无人及你 2021-01-30 21:00

How can I position my loading indicator in the center of the screen. Currently I\'m using a little placeholder and it seems to work fine. However, when I scroll down, the loadin

7条回答
  •  无人及你
    2021-01-30 21:41

    Here is a solution using an overlay that inhibits along with material design spinner that you configure one time in your app and you can call it from anywhere.

    app.component.html

    (put this somewhere at the root level of your html)

    app.component.css

    .plzWait{
      position: relative;
      left: calc(50% - 50px);
      top:50%;
    
    }
    .overlay{
      position: absolute;
      top:0px;
      left:0px;
      width: 100%;
      height: 100%;
      background: black;
      opacity: .5;
      z-index: 999999;
    }
    

    app.component.ts

    height = 0;
    width = 0;
    constructor(
        private message: MessagingService
      }
    ngOnInit() {
        this.height = document.body.clientHeight;
        this.width = document.body.clientWidth;
      }
    

    messaging.service.ts

    import { Injectable } from '@angular/core';
    import { Subject } from 'rxjs';
    
    @Injectable({
        providedIn: 'root',
      })
    export class MessagingService {
        // Observable string sources
      private plzWaitObservable = new Subject();
    
    
      // Public Observables you subscribe to
      public plzWait$ = this.plzWaitObservable.asObservable();
    
      public plzWait = (wait: boolean) => this.plzWaitObservable.next(wait);
    }
    

    Some other component

    constructor(private message: MessagingService) { }
    somefunction() {
        this.message.plzWait(true);
        setTimeout(() => {
                this.message.plzWait(false);
        }, 5000);
    }
    

提交回复
热议问题