【问题标题】:angular 2 play css animation when div/component is in viewport当 div/component 在视口中时,angular 2 播放 css 动画
【发布时间】:2017-11-28 23:56:08
【问题描述】:

我正在构建一个有角度的登录页面,在我的第三个 div 中,我有一些动画。第一个 div 占用 100vh 视图空间,另一个说 50,然后我有以下 div(所有这些 div 代表单个组件):

<div id="about" class="app-about-us-div-container">

    <div class="onboarding-flow mdl-grid">
        <div class="mdl-cell mdl-cell--4-col">
            <div class="circle circle-1">
            <i class="onboarding-icon material-icons">local_phone</i>
            </div>
            <div class="onboarding-text">
                <p>Tell us your problem domain and we will customize our solution to meet your specific needs</p>
            </div>
        </div>
        <div class="mdl-cell mdl-cell--4-col">
            <div class="circle circle-2">
                <i class="onboarding-icon material-icons">group</i>
            </div>
            <div class="onboarding-text">
                <p>Engage your project stakeholders, end users without any time hassle to build together a clear product vision</p>
            </div>
        </div>
        <div class="mdl-cell mdl-cell--4-col">
            <div class="circle circle-3">
                <i class="onboarding-icon material-icons">trending_up</i>
            </div>
            <div class="onboarding-text">
                <p>Benefit from our analytics to understand your users and their vision for the product. Build Personas to take best design decisions and streamline important product features </p>
            </div>
        </div>
    </div>

</div>

这是css:

.app-about-us-div-container{
    position: relative;
    height: 70vh;
    text-align: center;
    background-color: #ECEFF1;
}

.app-about-us-div-container h4{
    margin-top: 0%;
    padding-top: 20px;
}

.onboarding-flow {
    align-content: center;
    padding-top: 2%;
}

.circle {
    display: inline-block;
    width: 150px;
    height: 150px;
    box-sizing: border-box;
    -webkit-border-radius: 75px;
    -moz-border-radius: 75px;
    border-radius: 75px;
    background: transparent;
    text-align: center;
    border: 3px solid #cccccc;
    animation-name: pop;
    animation-iteration-count: 1;
    animation-duration: 0.3s;
    animation-direction: normal;
    animation-fill-mode: forwards;
    animation-timing-function: ease-in-out;
    color: #cccccc;
}
.circle-1 {
    animation-delay: 1s;
}
.circle-2 {
    animation-delay: 2s;
}
.circle-3 {
    animation-delay: 3s;
}

.onboarding-icon {
    align-self: center;
    font-size: 65px;
    position: relative;
    top: calc(50% - 35px);
}

.onboarding-text {
    position: relative;
    padding-top: 2%;
    width: 60%;
    left: 20%;
}
.onboarding-text p {
    font-size: 17px;
}

@keyframes pop {
    0% {
        color: #F44336;
        transform: scale(1);
    }

    50% {
        color: #ffffff;
        border-color: #F44336;
        background-color: #F44336;
        transform: scale(1.2);
    }

    100% {
        color: #ffffff;
        border-color: #EF5350;
        background-color: #EF5350;
        transform: scale(1);
    }
}

现在的问题是动画在页面加载时播放,直到用户到达这个 div 才结束。当这个 div 在视口中或屏幕顶部时,我如何确保播放这些动画(我不确定什么是最好的选择,因为上面的 div 占据 50vh 所以在某一点它们都是可见的 50 50)?在这种情况下,环形动画有什么帮助吗?

它的组件:

@Component({
  selector: 'app-about-us',
  templateUrl: './about-us.component.html',
  styleUrls: ['./about-us.component.css']
})
export class AboutUsComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

【问题讨论】:

    标签: html css angular


    【解决方案1】:

    我使用ng-in-viewport 来实现类似的效果。按照插件 GitHub 页面上的说明进行操作:

    1.安装插件

        npm install --save ng-in-viewport intersection-observer
    

    注意:intersection-observer 是一个用于支持所有主流浏览器的 polyfill

    2。使用示例

    在你的模块中:

    import { InViewportModule } from 'ng-in-viewport';

    import 'intersection-observer';

    在您的模板中:

    <div in-viewport (inViewportAction)="action($event)" class="circle circle-1">
          <i class="onboarding-icon material-icons">local_phone</i>
    </div>
    

    在您的组件中:

    action(event : any) {
            //do something with the element
    }
    

    例如,您可以使用 Angular's Renderer 向元素添加 css 类:

    import {Renderer2 } from '@angular/core';
    
    ...
    
    constructor(private renderer: Renderer2) {}
    action(event : any) {
        if(event.value){ //if element is in viewport
          this.renderer.addClass(event.target, "circle-1");
        }
    }
    

    【讨论】:

    • IntersectionObserverEntry 在 IE 中根本不支持。在支持 IE 的同时还有其他方法吗? developer.mozilla.org/en-US/docs/Web/API/…
    • 你必须为它添加一个 polyfill "import 'intersection-observer'; " in yout polyfill.ts @let_the_coding_begin
    • 关于 polyfill 和 IE11 的一个警告 - 性能通常非常糟糕。我不会把它放在这个插件上,只是 IE11 脚本很慢而且 polyfill 必须做很多工作。但在我们的应用程序中,它实际上只是让页面崩溃,即使在尝试去抖动和其他操作时也是如此。
    猜你喜欢
    • 2017-09-20
    • 2019-10-11
    • 1970-01-01
    • 2017-08-23
    • 2013-08-30
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多