我有div,我希望使用CSS过渡属性在3秒间隔内将宽度从0增加到100。当我在Chrome Developer工具中更改此属性时,它会在3秒的持续时间内从0到100很好地增长。但是,如果我应用组件的ngOnInit()
中的样式,它就是即时的。我做错了吗?
Component.ts:
@Component({
selector: 'notFound',
templateUrl: 'app/notFound.component/notFound.component.html',
directives: [ROUTER_DIRECTIVES]
})
export class NotFoundComponent {
ngOnInit() {
(<HTMLElement>document.querySelector('.determinate')).style.width = "100%";
}
}
Component.html:
<div class="wrapper">
<i class="material-icons error">error_outline</i>
<div class="not-found-text"> No such page 'round here.</div>
<a [routerLink]="['Menu']" class="waves-effect waves-light btn blue">
<i class="material-icons">home</i>
<!--home-->
</a>
<div class="progress">
<div class="determinate"></div>
</div>
</div>
<style>
.progress {
margin-top: 30px;
background: white;
}
.determinate {
width: 0%;
background: #2196F3;
transition: width 3s ease-in-out;
}
</style>
答案 0 :(得分:2)
我通过将呼叫包裹在0ms setTimeout
中来解决它。太可惜了。
ngOnInit() {
setTimeout(function () {
(<HTMLElement>document.querySelector('.determinate')).style.width = "100%";
}, 0);
}