Ответ 1
используйте position:fixed
вместо position:absolute
Первый - относительно вашего экрана. (не зависит от прокрутки)
Второй - относительно страницы. (зависит от прокрутки)
Примечание: IE6 не поддерживает позицию: исправлено.
Как я могу поместить индикатор загрузки в центр экрана. В настоящее время я использую небольшой заполнитель, и, похоже, он работает нормально. Однако, когда я прокручиваю вниз, индикатор загрузки остается в этом предопределенном положении. Как я могу заставить его следовать за прокруткой, чтобы он всегда сидел сверху?
#busy
{
position: absolute;
left: 50%;
top: 35%;
display: none;
background: transparent url("../images/loading-big.gif");
z-index: 1000;
height: 31px;
width: 31px;
}
#busy-holder
{
background: transparent;
width: 100%;
height: 100%;
}
используйте position:fixed
вместо position:absolute
Первый - относительно вашего экрана. (не зависит от прокрутки)
Второй - относительно страницы. (зависит от прокрутки)
Примечание: IE6 не поддерживает позицию: исправлено.
.loader{
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 9999;
background: url('//upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Phi_fenomeni.gif/50px-Phi_fenomeni.gif')
50% 50% no-repeat rgb(249,249,249);
}
<div class="loader"></div>
Это то, что я сделал для Angular 4:
<style type="text/css">
.centered {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transform: -webkit-translate(-50%, -50%);
transform: -moz-translate(-50%, -50%);
transform: -ms-translate(-50%, -50%);
color:darkred;
}
</style>
</head>
<body>
<app-root>
<div class="centered">
<h1>Loading...</h1>
</div>
</app-root>
</body>
изменить абсолютную позицию абзаца занятого на фиксированный
Работал для меня в angular 4
добавив style="margin:0 auto;"
<mat-progress-spinner
style="margin:0 auto;"
*ngIf="isLoading"
mode="indeterminate">
</mat-progress-spinner>
Вы можете использовать этот OnLoad или во время извлечения информации из БД
В HTML добавьте следующий код:
<div id="divLoading">
<p id="loading">
<img src="~/images/spinner.gif">
</p>
В CSS добавьте следующий код:
#divLoading {
margin: 0px;
display: none;
padding: 0px;
position: absolute;
right: 0px;
top: 0px;
width: 100%;
height: 100%;
background-color: rgb(255, 255, 255);
z-index: 30001;
opacity: 0.8;}
#loading {
position: absolute;
color: White;
top: 50%;
left: 45%;}
если вы хотите показать и скрыть от JS:
document.getElementById('divLoading').style.display = 'none'; //Not Visible
document.getElementById('divLoading').style.display = 'block';//Visible
Вот решение, использующее наложение, которое запрещает использование прядильщика дизайна материала, который вы настраиваете один раз в своем приложении, и вы можете вызывать его из любого места.
(поместите это где-нибудь на корневой уровень вашего HTML)
<div class="overlay" [style.height.px]="height" [style.width.px]="width" *ngIf="message.plzWait$ | async">
<mat-spinner class="plzWait" mode="indeterminate"></mat-spinner>
</div>
.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;
}
height = 0;
width = 0;
constructor(
private message: MessagingService
}
ngOnInit() {
this.height = document.body.clientHeight;
this.width = document.body.clientWidth;
}
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class MessagingService {
// Observable string sources
private plzWaitObservable = new Subject<boolean>();
// Public Observables you subscribe to
public plzWait$ = this.plzWaitObservable.asObservable();
public plzWait = (wait: boolean) => this.plzWaitObservable.next(wait);
}
constructor(private message: MessagingService) { }
somefunction() {
this.message.plzWait(true);
setTimeout(() => {
this.message.plzWait(false);
}, 5000);
}