【发布时间】:2016-02-06 16:34:16
【问题描述】:
我正在寻找类似于this example 的预引导加载屏幕,但适用于 Angular 2。
【问题讨论】:
标签: angular
我正在寻找类似于this example 的预引导加载屏幕,但适用于 Angular 2。
【问题讨论】:
标签: angular
我可以建议一种简单的 CSS 方法。
首先将.loading div 添加到主HTML 页面中,它应该跟随主应用程序组件元素。例如:
<my-app></my-app>
<div class="loading">
<h1>Loading...</h1>
</div>
现在您可以使用my-app:empty + .loading CSS 选择器定位和设置初始屏幕样式,并在应用启动后立即消失。示例:
/* default .loading styles, .loading should be invisible, opacity: 0, z-index: -1 */
.loading {
opacity: 0;
transition: opacity .8s ease-in-out;
position: fixed;
height: 100%;
width: 100%;
top: 0;
left: 0;
background: #000;
z-index: -1;
}
/* .loading screen is visible when app is not bootstrapped yet, my-app is empty */
my-app:empty + .loading {
opacity: 1;
z-index: 100;
}
如果您将所有繁重的脚本放在关闭 body 标记之前,并且只在头部保留加载屏幕所需的最少样式以便它尽快显示然后脚本开始加载,则此方法效果更好。
这是一个简单的演示:
【讨论】:
my-app之间的空白不再为空,因为没有完全加载:transition: opacity 0 ease-in-out 1s;
我使用 FontAwesome 微调器采用了一种简单的方法(虽然我也喜欢其他答案):
<app-root>
<div class="text-center">
<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
<div>Something witty for your userbase</div>
</div>
</app-root>
【讨论】:
为什么不试试这个方法:
<app-root>
<style type="text/css">
#pre-bootstrap {
background-color: #262626;
bottom: 0px;
left: 0px;
position: fixed;
right: 0px;
top: 0px;
z-index: 999999;
}
#pre-bootstrap div.messaging {
color: #FFFFFF;
font-family: monospace;
left: 0px;
margin-top: -37px;
position: absolute;
right: 0px;
text-align: center;
top: 50%;
}
#pre-bootstrap h1 {
font-size: 26px;
line-height: 35px;
margin: 0px 0px 20px 0px;
}
#pre-bootstrap p {
font-size: 18px;
line-height: 14px;
margin: 0px 0px 0px 0px;
}
</style>
<div id="pre-bootstrap">
<div class="messaging">
<h1>
App is Loading
</h1>
<p>
Please stand by for your ticket to awesome-town!
</p>
</div>
</div>
</app-root>
【讨论】:
你可以这样做:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css">
app-root:empty::after {
content: 'Loading…';
width: 100px;
height: 100px;
position: absolute;
display: block;
top: 50%;
left: 50%;
font-size: 20px;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<app-root></app-root>
</body>
</html>
非常简单的方法。当然,您可以用加载器图像替换文本。
【讨论】:
我想最好的方法是在加载 div HTML 中使用内联样式(以显示)。然后在 css 中你有一个类来隐藏并使用 ngOnInit 将该类包含在加载 div 中。
在这种情况下,我们将首先让 html 呈现样式.. 显示加载,毕竟(下载 css 并运行 js)它将具有隐藏它的类。
export class AppComponent implements OnInit {
public loaded=false;
ngOnInit() {
this.loaded=true;
}
}
.loading.loaded {
z-index: -1;
opacity: 0;
}
<div class="loading"
style="opacity: 0.9;
color: #EEE;
position: absolute;
top: 0;
width: 100%;
text-align: center;
transition: opacity .8s ease-in-out;
height: 100%;
background: #fff;
z-index: 100;
display: table;"
[class.loaded]="loaded">
<md-spinner
style="display: table-cell;
vertical-align: middle;"
></md-spinner>
</div>
我不得不去掉角度视图内组件的可见性,因为它们在加载时没有任何样式加载(就像没有样式的 flickr 一样)。所以在css中我隐藏并在app.html中我显示
footer, header, home-view, homebuyer-view, homeseller-view, homecommerce-view,
.mat-select-placeholder{
visibility: hidden !important;
}
<style *ngIf="loaded">
footer, header,home-view, homebuyer-view, homeseller-view, homecommerce-view,
.mat-select-placeholder{
visibility: visible !important;
}
</style>
【讨论】:
在 Angular 项目中添加启动画面所需的只是两个步骤:
1- 将此 CSS 代码添加到 index.html 的 head 标记中:
<style>
.splash {
opacity: 1;
z-index: 1;
visibility: visible;
transition: opacity 1.4s ease-in-out, visibility 1.4s;
position: fixed;
height: 100%;
width: 100%;
top: 0;
left: 0;
background: #1e85c8;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
app-root:not(:empty) + .splash {
opacity: 0;
visibility: hidden;
}
.splash_image {
width: 100%;
max-width: 200px;
}
</style>
2- 然后将此 HTML 行添加到 app-root 选择器中:
<div class="splash">
<img class="splash_image" src="assets/images/logo.svg" alt="" />
</div>
注意:更改 src 以获得有效的徽标路径...
这样,当Angular设置app-root的内容时,loader会自动消失,我认为这是Angular中启动画面的最佳方式。
index.html 文件的完整代码如下:
<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Project Title</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<style>
.splash {
opacity: 1;
z-index: 1;
visibility: visible;
transition: opacity 1.4s ease-in-out, visibility 1.4s;
position: fixed;
height: 100%;
width: 100%;
top: 0;
left: 0;
background: #1e85c8;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
app-root:not(:empty) + .splash {
opacity: 0;
visibility: hidden;
}
.splash_image {
width: 100%;
max-width: 200px;
}
</style>
</head>
<body>
<app-root>
<div class="splash">
<img class="splash_image" src="assets/images/logo.svg" alt="" />
</div>
</app-root>
</body>
</html>
【讨论】: