javascript - 在 Y 屏幕位置显示 CSS 动画

标签 javascript jquery html css

访问者在屏幕 Y 位置滚动后,是否有任何可能的方法来显示 css 动画。 例如。我有 2 个宽度为 100% 的部分,在第一部分中,有一些图像、文本等。但在第二部分中,有 2 个图像应该在访问者在该 Y 位置上滚动时设置动画。(y 位置应包含图片的高度)。

这是我的代码示例:

[http://codepen.io/anon/pen/mVZJRy][1]

(这些图片是.png)

HTML 代码

<section class="section3">

      <div id="left"> <img src="img/blue.png"> </div>
      <div id="right"> <img src="img/black.png"> </div>

    </div>

CSS 代码

    *{
   padding:0;
  margin:0;
  }


.section1{
  width:100%;
  height:670px;
  text-align:left;
  background-color:white;
  border-bottom:2px solid rgba(0, 0, 0, 0.1);
  box-shadow:0px 2px 0px #DFDFDF;
  }

.section3{
    width:100%;
    height:550px;
    background-color:#f2f2f2;
}

#right{
   position:absolute;
   left:51vw;
   top:856px;
   width:400px;
   height:400px;
  }

#right img{
   width:170px;
   height:290px;
 opacity:0;
  animation-fill-mode: forwards;
  animation-delay:1.1s;
  animation-name: slideLeft;
  -webkit-animation-name: slideLeft;    
  animation-duration: 1s;   
  -webkit-animation-duration: 1s;
  animation-timing-function: ease-in-out;   
  -webkit-animation-timing-function: ease-in-out;       
  visibility: visible !important;   
}

@keyframes slideLeft {
    0% {
        transform: translateX(150%); opacity:1;
    }
    50%{
        transform: translateX(-8%);opacity:1;
    }
    65%{
        transform: translateX(4%);opacity:1;
    }
    80%{
        transform: translateX(-4%);opacity:1;
    }
    95%{
        transform: translateX(2%);opacity:1;
    }           
    100% {
        transform: translateX(0%);opacity:1;
    }
}

@-webkit-keyframes slideLeft {
    0% {
        -webkit-transform: translateX(150%);opacity:1;
    }
    50%{
        -webkit-transform: translateX(-8%);opacity:1;
    }
    65%{
        -webkit-transform: translateX(4%);opacity:1;
    }
    80%{
        -webkit-transform: translateX(-4%);opacity:1;
    }
    95%{
        -webkit-transform: translateX(2%);opacity:1;
    }           
    100% {
        -webkit-transform: translateX(0%);opacity:1;
    }
}

  #left{
   position:absolute;
   width:300px;
   height:357px;
   left:39vw;
   top:850px;
  }

  #left img{
   width:300px;
   height:357px;
animation-fill-mode: forwards;
  opacity:0;
  animation-delay:1.1s;

    animation-name: b;
    -webkit-animation-name: b;  

    animation-duration: 1s; 
    -webkit-animation-duration: 1s;

    animation-timing-function: ease-in-out; 
    -webkit-animation-timing-function: ease-in-out;     

    visibility: visible !important; 
}

@keyframes b {
    0% {
        transform: translateX(-150%);opacity:1
    }
    50%{
        transform: translateX(8%);opacity:1
    }
    65%{
        transform: translateX(-4%);opacity:1
    }
    80%{
        transform: translateX(4%);opacity:1
    }
    95%{
        transform: translateX(-2%);opacity:1
    }           
    100% {
        transform: translateX(0%);opacity:1
    }   
}


(-4%);opacity:1;
    }
    95%{
        -webkit-transform: translateX(2%);opacity:1;
    }           
    100% {
        -webkit-transform: translateX(0%);opacity:1;
    }
}

最佳答案

当滚动到正确的位置时,您可以为图像添加一些类名。并在这个类上设置你的 css animaion。 可能是这样的:

window.onscroll = function() {
  var scrolled = window.pageYOffset || document.documentElement.scrollTop,
      imgRight = document.getElementById('right').children[0],
      imgLeft= document.getElementById('left').children[0];

  if (scrolled => imgRight.height) {
      imgRight.className = 'slideLeft';
  }
  if (scrolled => imgLeft.height) {
      imgLeft.className = 'b';
  }
}

我在 css 中添加了一些修正 http://codepen.io/anon/pen/gPNpzM?editors=0110

关于javascript - 在 Y 屏幕位置显示 CSS 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35528487/

相关文章:

javascript - 如何根据下拉选择的 div 可见性制作所需的文本框?

javascript - 如何阻止 JS 驱动的整页滚动跳转到开始/结束

javascript - VideoJS响应两个视频

javascript - 包含外部网站的 iframe 不会触发 onscroll 事件处理程序

javascript - 在 JavaScript 中检查字符串是否包含另一个子字符串的最快方法?

javascript - 如何设置浏览器确认框焦点为取消?

html - 通过 CSS 设置 <table> 中第一列和第二列的对齐方式和填充

javascript - jQuery 正则表达式验证

javascript - 如何将 PHP 中的对象转换为 Javascript 中的数组?

html - Navbar div 的位置为 :fixed; 时创建的空白