Angular ng引导折叠:如何应用动画?
我使用的是折叠: 但是,它不具有动画效果;甚至不在演示网站上。我应该如何实现这个???因为他们使用“display:none”来隐藏和“display:block”来显示元素,所以不能应用“transition”CSS属性 因此,强制显示块、管理高度和不透明度以切换隐藏/显示:Angular ng引导折叠:如何应用动画?,angular,ng-bootstrap,Angular,Ng Bootstrap,我使用的是折叠: 但是,它不具有动画效果;甚至不在演示网站上。我应该如何实现这个???因为他们使用“display:none”来隐藏和“display:block”来显示元素,所以不能应用“transition”CSS属性 因此,强制显示块、管理高度和不透明度以切换隐藏/显示: .collapse, .collapse.in { display: block !important; transition: all .25s ease-in-out; } .collapse { opa
.collapse, .collapse.in {
display: block !important;
transition: all .25s ease-in-out;
}
.collapse {
opacity: 0;
height: 0;
}
.collapse.in {
opacity: 1;
height: 100%;
}
使用基本过渡和不透明度/高度,它看起来更平滑
您可以使用关键帧制作自己的动画,并应用于.collapse.in以获得更好的切换隐藏/显示体验
然后,如果您在项目中使用Angular 2,您可以切换到ng2引导:在您的组件中,您可以添加如下内容:
animations: [
trigger('expandCollapse', [
state('open', style({height: '100%', opacity: 1})),
state('closed', style({height: 0, opacity: 0})),
transition('* => *', [animate('100ms')])
]),
]
<div [ngbCollapse]="isCollapsed" [@expandCollapse]="isCollapsed ? 'closed' : 'open'"> ... </div>
动画:[
触发器('expandCollapse'[
状态('open',样式({height:'100%',不透明度:1})),
状态('closed',样式({height:0,opacity:0})),
过渡('*=>*',[animate('100ms')])
]),
]
...
在这里查看更多详细信息,我认为这是一个很好的方法,它可以用于显示和折叠:(尽管它不再需要ng引导) template.html:
<button (click)="isCollapsed = !isCollapsed">Toggle</button>
<p [@smoothCollapse]="isCollapsed?'initial':'final'">
your data here
</p>
import { trigger, state, style, animate, transition } from '@angular/animations';
@Component({
selector: 'app-my-component',
templateUrl: './template.html',
styleUrls: ['./style.scss'],
animations: [
trigger('smoothCollapse', [
state('initial', style({
height:'0',
overflow:'hidden',
opacity:'0'
})),
state('final', style({
overflow:'hidden',
opacity:'1'
})),
transition('initial=>final', animate('750ms')),
transition('final=>initial', animate('750ms'))
]),
]
})
export class MyComponent ...
<button class="btn btn-primary" (click)="collapsed = !collapsed">
Toggle
</button>
<div [appCollapseAnimated]="collapsed" class="beautiful-div border border-secondary mt-5">
this should collapse with the default bootstrap behaviour
</div>
详情:
- 初始高度:0允许从零开始
- 如果不指定最终高度,则当元素全部显示时,该元素将停止增长
- 溢出:隐藏在任何地方,因此您的元素不会在其他元素上运行
- 从0到1的不透明度使它更好(在我看来)
- 我花了一些时间才意识到的一件重要事情是,不要将
放在[ngbCollapse]=“isCollapsed”
中,否则会破坏所有动画。我们不需要它,因为我们将高度设置为0
希望有帮助,我花了一天的时间:p我创建了一个指令,它只使用引导类,只使用角度动画执行相同的原始引导效果,请查看 指令代码
import {Directive, ElementRef, HostBinding, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {animate, AnimationBuilder, AnimationPlayer, keyframes, style} from '@angular/animations';
@Directive({
selector: '[appCollapseAnimated]'
})
export class CollapseAnimatedDirective implements OnChanges, OnInit {
private static readonly SHOW_STYLE = 'show';
private static readonly COLLAPSING = 'collapsing';
@Input('appCollapseAnimated')
collapsed = true;
@Input()
skipClosingAnimation = false;
@HostBinding('class.collapse')
private readonly addCollapseClass = true;
private currentEffect: AnimationPlayer;
private _closeEffect: AnimationPlayer;
private _openEffect: AnimationPlayer;
constructor(private el: ElementRef,
private builder: AnimationBuilder) {
}
ngOnInit(): void {
if (!this.collapsed) {
this.getClassList().add(CollapseAnimatedDirective.SHOW_STYLE);
}
}
private get openEffect(): AnimationPlayer {
if (!this._openEffect) {
this._openEffect = this.builder.build(animate('500ms', keyframes([
style({height: '0'}),
style({height: '*'}),
]))).create(this.el.nativeElement);
}
this._openEffect.onDone(() => this.effectDone());
return this._openEffect;
}
private get closeEffect(): AnimationPlayer {
if (!this._closeEffect) {
this._closeEffect = this.builder.build(animate('500ms', keyframes([
style({height: '*'}),
style({height: '0'}),
]))).create(this.el.nativeElement);
}
this._closeEffect.onDone(() => this.effectDone());
return this._closeEffect;
}
private effectDone() {
if (this.collapsed) {
this.getClassList().remove(CollapseAnimatedDirective.SHOW_STYLE);
}
this.getClassList().remove(CollapseAnimatedDirective.COLLAPSING);
if (this.currentEffect) {
this.currentEffect.reset();
this.currentEffect = null;
}
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.collapsed && !changes.collapsed.firstChange) {
if (changes.collapsed.previousValue === true && changes.collapsed.currentValue === false) {
this.startOpening();
}
if (changes.collapsed.previousValue === false && changes.collapsed.currentValue === true) {
this.startClosing();
}
}
}
private startOpening(): void {
this.getClassList().add(CollapseAnimatedDirective.SHOW_STYLE);
const effect = this.openEffect;
this.playEffect(effect);
}
private getClassList() {
const nativeElement = this.el.nativeElement as HTMLElement;
return nativeElement.classList;
}
private startClosing(): void {
const effect = this.closeEffect;
if (this.skipClosingAnimation) {
this.effectDone();
} else {
this.playEffect(effect);
}
}
private playEffect(effect: AnimationPlayer) {
if (!this.currentEffect) {
this.getClassList().add(CollapseAnimatedDirective.COLLAPSING);
this.currentEffect = effect;
this.currentEffect.play();
}
}
}
在模板中使用它:
<button (click)="isCollapsed = !isCollapsed">Toggle</button>
<p [@smoothCollapse]="isCollapsed?'initial':'final'">
your data here
</p>
import { trigger, state, style, animate, transition } from '@angular/animations';
@Component({
selector: 'app-my-component',
templateUrl: './template.html',
styleUrls: ['./style.scss'],
animations: [
trigger('smoothCollapse', [
state('initial', style({
height:'0',
overflow:'hidden',
opacity:'0'
})),
state('final', style({
overflow:'hidden',
opacity:'1'
})),
transition('initial=>final', animate('750ms')),
transition('final=>initial', animate('750ms'))
]),
]
})
export class MyComponent ...
<button class="btn btn-primary" (click)="collapsed = !collapsed">
Toggle
</button>
<div [appCollapseAnimated]="collapsed" class="beautiful-div border border-secondary mt-5">
this should collapse with the default bootstrap behaviour
</div>
切换
这将使用默认引导行为崩溃
我使用过ng2引导,当时出现了一些问题,这就是为什么我改用了ng引导。无论如何,谢谢。它有问题的手风琴描述与标签点击链接这类作品,但它动画我的元素的大小只有一次增长。之后,展开立即发生,只有不透明效果似乎起作用。有什么线索吗?试着把过渡('*=>',[animate('100ms')])改成过渡('*',[animate('100ms')])我花了大约4个小时制作简单的下拉式动画,这是我能得到的最接近的。非常感谢。这里还有一个带有关键帧的解决方案:它起作用了,但部分起作用。从初始状态到最终状态的动画可见,但从最终状态到初始状态的动画不会发生。