如何创建一个Material Design径向反应动画

8
我希望我的工具栏可以使用Material Design径向反应编排指南来更改颜色。

Radial Reaction

我希望将其作为Angular 2转换实现,但我不知道如何准确地做到这一点:
它应该看起来像这样...
@Component({
 ...
  animations: [
    trigger('heroState', [
    state('inactive', style({
      backgroundColor: '#eee',
      transform: 'scale(1)'
     })),
     state('active',   style({
       backgroundColor: '#cfd8dc',
       transform: 'scale(1.1)'
     })),
     transition('inactive => active', animate('100ms ease-in')),
     transition('active => inactive', animate('100ms ease-out'))
   ])
  ]
})
2个回答

8

更新: 已更新的PLUNKER, 使用box-shadow进行animation

@Component({
  selector: 'my-app',
  template: `
    <div class="head" [@mtSlide]="activeSlide == 1 ? 'active': 'inactive'">
      <input id="searchBar" type="search" [@mtTranslate]="activeSlide == 2 ? 'active': 'inactive'">

      <i class="fa fa-bars" [@mtRotate]="activeSlide == 1 ? 'active': 'inactive'" (click)="menuOpen()" [style.z-index]="activeSlide == 1 ? 1 : 0"></i>
      <i class="fa fa-arrow-left" [@mtRotate]="activeSlide == 2 ? 'active': 'inactive'" (click)="activeSlide = 1" [style.z-index]="activeSlide == 2 ? 1 : 0"></i>

      <i class="fa fa-search" [@mtScale]="activeSlide == 1 ? 'active': 'inactive'" style="right:10px; left: initial;"  (click)="activeSlide = 2"></i>
    </div>
  `,
  animations: [
    trigger('mtSlide', [
    state('inactive', style({
      'box-shadow': 'rgb(0, 102, 255) 0px 0px 0px 0px inset, rgb(0, 0, 0) 0px 2px 8px -3px'
    })),
    state('active', style({
      'box-shadow': 'rgb(0, 102, 255) 100vw 0px 0px 0px inset, rgb(0, 0, 0) 0px 2px 8px -3px'
    })),
    transition('inactive <=> active', animate('200ms ease-out'))
    ]),

    trigger('mtTranslate', [
    state('inactive', style({
      transform: 'translateX(100%)'
    })),
    state('active', style({
      transform: 'translateX(0)'
    })),
    transition('inactive <=> active', animate('200ms ease-out'))
    ]),

    trigger('mtRotate', [
    state('inactive', style({
      transform: 'rotateZ(-90deg)'
      opacity: 0;
    })),
    state('active', style({
      transform: 'rotateZ(0)';
      opacity: 1;
    })),
    transition('inactive <=> active', animate('300ms ease-out'))
    ]),

    trigger('mtScale', [
    state('inactive', style({
      transform: 'scale(0)'
    })),
    state('active', style({
      transform: 'scale(1)';
    })),
    transition('inactive <=> active', animate('400ms ease-out'))
    ])],

  styles: [`
    * {
      box-sizing: border-box;
    }

    .head {
      position: relative;
      font-size: 18px;
    }

    .head, .color-bar, .head > input {
      width: 100%;
      height: 50px;
    }

    .head i, .head > input{
      position: absolute;
    }

    .head i {
      line-height: 50px;
      cursor: pointer;
      color: white;
      padding: 0 10px;
      width: 50px;
      text-align: center;
      left: 10px;
    }

    .head i.fa-arrow-left {
      color: #111;
    }

    .head > input {
      border: 0;
      outline: 0;
      padding-left: 50px;
    }
  `]
})
export class App {
  activeSlide = 1;

  menuOpen() {
    alert('Menu Clicked');
  }
}

非常好的工作,但实际上我想只保留一个div,并改变它的颜色,而不是用另一个div替换它...使用CSS过渡是否可能实现? - Marcos J.C Kichel
是的,可以使用box-shadow: inset来实现这个效果,直接应用于 .head div,然后调整阴影区域即可。 - Ankit Singh
你能否更新或者更好地补充一下你的回答,这样我就可以尝试并接受它了吗? - Marcos J.C Kichel
在这里,只需一个div就可以实现。 - Ankit Singh
很酷,我觉得我可以将你做的东西适应到我的特殊情况中,谢谢 :) - Marcos J.C Kichel
显示剩余2条评论

3

您可以使用CSS来模仿这个设计,特别是伪元素。它不需要任何JS,但如果您想添加多种颜色等,则可能需要使用JS。

.menu {
  height: 50px;
  width: 100%;
  background: lightgray;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
}
.menu .button1 {
  position: absolute;
  top: 20%;
  left: 10px;
  height: 30px;
  width: 30px;
  background: tomato;
  cursor: pointer;
}
.menu .button1:before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: tomato;
  transition: all 1s;
  height: 0;
  width: 0;
  cursor: initial;
}
.menu .button1:hover:before {
  height: 300vw;
  width: 300vw;
}
<div class="menu">
  <div class="button1"></div>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接