【问题标题】:How to add transition to this div如何向此 div 添加过渡
【发布时间】:2019-02-11 08:16:20
【问题描述】:
.login-error {
    text-align: center;
    margin-bottom: 15px;
}

.login-error span {
    background: #e03030;
    padding: 2px 28px 2px 28px;
}

ts 文件

focusPasswordInput() {
    this.loginForm.controls.password.markAsUntouched();
}

focusEmailInput(){
    this.loginForm.controls.email.markAsUntouched();
}

该脚本在提交表单时添加一个 div,如果 ng if 条件为真,并在 span 内显示错误。如果用户在单击按钮提交表单后将焦点放回输入框内,则上述 ts 文件中的两个函数会隐藏错误消息。 它适用于错误 div 隐藏和显示,但它看起来并不漂亮所以有没有办法添加过渡?

<div *ngIf="(loginError && loginForm.controls.email.touched) && (loginForm.controls.password.touched) " class="login-error">
   <span>Email or Password incorrect</span>
</div>
<input type="text" formControlName="email" name="email" (focus)="focusEmailInput()" placeholder="email *"/>
<input type="text" formControlName="password" (focus)="focusPasswordInput()" placeholder="Password *"/>

【问题讨论】:

标签: css angular


【解决方案1】:

您可以使用角度动画为错误消息设置动画:https://www.npmjs.com/package/angular-animations

<div [@zoomInOnEnter]
    [@zoomOutOnLeave]
    *ngIf="(loginError && loginForm.controls.email.touched) && (loginForm.controls.password.touched)"
    class="login-error">
    <span>Email or Password incorrect</span>
</div>

记得在 .ts 文件中添加函数定义:

@Component({
    selector: '...',
    templateUrl: '...',
    styleUrls: ['...'],
    animations: [
        zoomInOnEnterAnimation(),
        zoomOutOnLeaveAnimation()
    ]
})

【讨论】:

  • 虽然我们可以在 div 消失时对高度进行某种过渡,因为它看起来并不平滑
猜你喜欢
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
相关资源
最近更新 更多