Angular2动画媒体查询

Bre*_*han 8 javascript css animation angular

我一直在玩Angular2的动画DSL,我对如何将动画限制为特定的媒体屏幕大小感到困惑.

例如,假设我在主页上有400px宽的徽标,当用户访问计算机显示器上的任何其他页面时,缩小到200px宽.

...
animations: [
trigger('homeLogoState',[
    state('inactive', style({
      width: '200px',
      transition: 'width'
    })),
    state('active', style({
      width: '400px',
      transition: 'width'
    })),
    transition('inactive <=> active', animate('300ms ease-in'))
  ])
]
...
Run Code Online (Sandbox Code Playgroud)

然而,在移动设备上,每页需要保持100px.

我理解我可以通过控制DOM中显示的内容来控制不同的动画,如下所示,但这可能会产生大量的代码重复,以处理每个屏幕大小的每个动画变化.

<div class="hidden-under-1920px" @logoAnimationOne="page">
  <img src="logo.png">
</div>
<div  class="hidden-under-1280px" @logoAnimationTwo="page">
  <img src="logo.png">
</div>
Run Code Online (Sandbox Code Playgroud)

将不同的动画约束到特定的@media选择器大小的正确方法是什么?

小智 3

我有一个解决方案,只是不知道它是否是最好的。我有类似的问题,但解决了。

我有一个变量,表示它是打开还是关闭(menuOpen)仅在 true 和 false 之间变化,还有一个具有三种状态的变量:0或1或2(onOpen)我有三种状态,你可以在这里看到它

import { Component, trigger, state, animate, transition, style, HostListener} from '@angular/core'
        ....
        ....
        animations: [
                trigger('visibilityChanged', [
                    state('0', style({ width: '50px' })),
                    state('1', style({ width: '25%' })),
                    state('2', style({ width: '100%' })),
                    transition('* => *', animate('.3s'))
                ])
            ]....
Run Code Online (Sandbox Code Playgroud)

你可以做一个单一的功能来解析,我的是,我没有做过

    export class AppComponent {
        wt;

        @HostListener('window:resize', ['$event'])
        sizeWindow(event) {
            this.wt = event.target.innerWidth;
            this.sizeMenu(this.wt);
            console.log('width =>', this.wt);
        }

        constructor() {
            this.wt = window.innerWidth;
        }

        sizeMenu(width) {
            if (this.menuOpen === true) {
                if (width >= 600) {
                    this.onTestOpen = 1;

                } else if (width < 600) {
                    this.onTestOpen = 2;
                }

            } else if (this.menuOpen === false) {
                this.onTestOpen = 0;

            }

          }

        openMenu() {
                let wwt = window.innerWidth;
                if (this.menuOpen === false) {
                    if (wwt >= 600) {
                        this.onTestOpen = 1;

                    } else if (wwt < 600) {
                        this.onTestOpen = 2;
                    }
                    this.menuOpen = true;

                } else if (this.menuOpen === true) {

                    this.onTestOpen = 0;
                    this.menuOpen = false;

                }

            }
    }
Run Code Online (Sandbox Code Playgroud)

在我的模板中我有它

<div class="geral" [@visibilityChanged]="onOpen"></div>
Run Code Online (Sandbox Code Playgroud)

我认为就你的情况而言,将不得不与更多的州打交道。