【问题标题】:Fly-in animation only on focus飞入动画仅在焦点上
【发布时间】:2020-05-03 07:31:37
【问题描述】:

我想在 Angular 应用程序中创建 fly-in 效果,仅在具有特定类(而不是页面加载)的 块焦点 上。 这可能没有 jquery 或其他库(我搜索但实际上只找到了带有 jquery 的解决方案,我没有在我的 Angular 应用程序上使用它)。 我用 Angular 标签问是因为我在一个 Angular 应用程序中,但也许还有一种只使用 css 的方法?

一个不适用于焦点但页面加载的示例:

#first-page {
  height: 100vh;
  background-color: red;
  color: white;
  line-height: 100%;
}
#second-page {
  height: 100vh;
  background-color: green;
  color: white;
  line-height: 100%;
}
.fly-in-block {
  background-color: blue;
  width: 100px;
  height: 100px;
  margin: 100px auto;
  transform: translateX(-600%);
  -webkit-transform: translateX(-600%);
}
.flyIn {
  animation: flyIn 0.8s forwards;
  -webkit-animation: flyIn 0.8s forwards;
}
@keyframes flyIn {100% { transform: translateX(0%); }}
@-webkit-keyframes flyIn {100% { -webkit-transform: translateX(0%); }}
<div id="first-page">
  First page
</div>
<div id="second-page">
  Second page
  <div class="fly-in-block flyIn"></div>
</div>

这里是 Angular sn-p 的链接: https://stackblitz.com/edit/angular-fly-in-animation-on-focus?file=src%2Fapp%2Fapp.component.css

【问题讨论】:

    标签: javascript css angular sass


    【解决方案1】:

    我不确定你所说的专注是什么意思。你在寻找这样的东西吗?您可能可以根据自己的需要对其进行调整。

    在 html 中查看我删除了 flyIn 类并仅在块滚动到视图时通过 js 添加它。

    一旦动画被触发,监听器就会被移除。

    const block = document.querySelector( '.fly-in-block' )
    const blockOffset = block.offsetTop
    
    const triggerAnimation = () => {
      const scrollHeight = document.body.scrollHeight
      if( scrollHeight >= blockOffset ) {
        block.classList.add( 'flyIn' )
        window.removeEventListener( 'scroll', triggerAnimation )
      }
    }
    
    window.addEventListener( 'scroll', triggerAnimation )
    #first-page {
      height: 100vh;
      background-color: red;
      color: white;
      line-height: 100%;
    }
    #second-page {
      height: 100vh;
      background-color: green;
      color: white;
      line-height: 100%;
    }
    .fly-in-block {
      background-color: blue;
      width: 100px;
      height: 100px;
      margin: 100px auto;
      transform: translateX(-600%);
      -webkit-transform: translateX(-600%);
    }
    .flyIn {
      animation: flyIn 0.8s forwards;
      -webkit-animation: flyIn 0.8s forwards;
    }
    @keyframes flyIn {100% { transform: translateX(0%); }}
    @-webkit-keyframes flyIn {100% { -webkit-transform: translateX(0%); }}
    <div id="first-page">
      First page
    </div>
    <div id="second-page">
      Second page
      <div class="fly-in-block"></div>
    </div>

    【讨论】:

    • 这是我需要的,谢谢。很遗憾,只有CSS没有解决方案,但也许我要求太多了。
    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多