简体   繁体   English

Angular2中的jQuery .slideUp()和.slideDown()等价物

[英]jQuery .slideUp() and .slideDown() equivalent in Angular2

Can you please guide me on how to create a custom animation in Angular2 equivalent to jQuery .slideUp() and .slideDown() . 你能否指导我如何在Angular2中创建一个等同于jQuery .slideUp().slideDown()的自定义动画。 I know I can use jQuery plugin for this, but many folks recommend not to use it. 我知道我可以使用jQuery插件,但许多人建议不要使用它。 So I'm looking for an alternate method to create these animations. 所以我正在寻找另一种方法来创建这些动画。 The slideUp and slideDown will make any div collapsible with smooth animation on click of some button. slideUp和slideDown将使任何div可以在点击某个按钮时与平滑动画折叠。

In order to create a custom slideUp/Down for an element we can do the following: 要为元素创建自定义slideUp / Down,我们可以执行以下操作:

1) Define two states (open, close) with according styles for height . 1)使用相应的height样式定义两个状态(打开,关闭)。 You could also add other styles like opacity or color . 您还可以添加其他样式,如opacitycolor

We use the special value * to tell Angular to use the value during execution. 我们使用特殊值*告诉Angular在执行期间使用该值。 This will include margins and paddings. 这将包括边距和填充。

2) Add a transition to tell Angular how it should go from one state to the other. 2)添加一个过渡来告诉Angular它应该如何从一个状态转到另一个状态。 This will define how intermediate values (tweens) will be calculated. 这将定义如何计算中间值(补间)。 Syntax follows {{total duration}} [{{delay}}] [{{easing function}}] . 语法遵循{{total duration}} [{{delay}}] [{{easing function}}] Time can be expressed in seconds or milliseconds. 时间可以用秒或毫秒表示。 Eg: .2s, 200ms . 例如: .2s, 200ms Default easing function when omitted is ease . 省略时默认的缓动功能ease

See below: 见下文:

@Component({
  selector: 'my-app',
  template: `
    <h1>App</h1>
    <button (click)="toggle()">Toggle</button>
    <div class="card" [@toggle]="state">
      Click to slideUp/Down
    </div>
  `,
  animations: [
    trigger('toggle', [
      state('open', style({ height: '200px' })),
      state('closed', style({ height: '*' })),
      transition('open <=> closed', animate('200ms ease-in-out'))
    ])
  ],
})
export class App {
  state = "open";
  toggle() {
    this.state = (this.state=="open")? "closed": "open";
  }
}

Plunker Plunker

Below is the animation for slide down and up using dynamic height values. 下面是使用动态高度值向下和向下滑动的动画。

If there is no margin and/or padding on the object, you can remove those settings. 如果对象上没有边距和/或填充,则可以删除这些设置。

animations: [
  trigger('slideUpDown', [
    transition('void => *', [
      style({height: 0, margin: 0, padding: 0, opacity: 0}),
      animate(500, style({height: '*', margin: '*', padding: '*', opacity: 1}))
    ]),
    transition('* => void', [
      style({height: '*', margin: '*', padding: '*', opacity: 1}),
      animate(500, style({height: 0, margin: 0, padding: 0, opacity: 0}))
    ])
  ])
] 

Pure CSS approach: 纯CSS方法:

html HTML

<button (click)="toggleSlide=!toggleSlide">Toggle</button>
<div class="slider closed" [ngClass]="{'closed':!toggleSlide}">
  <div class="content"></div>
</div>

css CSS

.slider {
  max-height: 1000px;
  overflow: hidden;
  transition: max-height 0.5s cubic-bezier(1,0,1,0);
}

.slider.closed {
  max-height: 0;
  transition: max-height 0.7s cubic-bezier(0,1,0,1);
}

.content{
  height: 200px;
  width: 200px;
  background-color: blue;
}

Plunker example here 这里有 Plunker的例子

You can use Element.animate() 你可以使用Element.animate()

 div > div { width: 300px; height: 200px; display: block; background: blue; } 
 <div> click <div></div> </div> <script> const div = document.querySelector("div"); const props = ["200px", "0px"]; // from, to div.onclick = () => { div.firstElementChild.animate({ height: props }, { duration: 1000, easing: "ease-in-out", iterations: 1, fill:"forwards" }) .onfinish = function() { props.reverse() } } </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM