是否可以在滚动条上设置flexbox元素的动画?

时间:2019-04-14 16:18:55

标签: javascript css css3 flexbox css-transitions

是否可以向上滚动动画弹性元素?

我正在努力实现这一目标:https://codepen.io/Sergiop79/pen/bxjGEe

具有以下元素(在flexbox中设置样式),可以是整个“行”或单个“ col”。

我无法在Flexbox上找到与此有关的任何内容,所以我只想知道是否有可能。谢谢!

<about id="about" class="row">
      <div class="col">
        <div class="about1">
          <img src="1-AB.svg">
        </div>
        <div class="about1a">
          <h3>About</h3>
          <p>Now that we know who you are, I know who I am. I'm not a mistake! It all makes sense! In a comic, you know how you can tell who the arch-villain's going to be? He's the exact opposite of the hero.</p>
        </div>
      </div>
      <div class="col">
        <div class="about2">
          <img src="2-AB.svg">
        </div>
        <div class="about2a">
            <h3>Aboot</h3>
            <p>Now that we know who you are, I know who I am. I'm not a mistake! It all makes sense! In a comic, you know how you can tell who the arch-villain's going to be? He's the exact opposite of the hero.</p>
          </div>
      </div>
      <div class="col">
        <div class="about3">
          <img src="3-AB.svg">
        </div>
        <div class="about3a">
            <h3>Abute</h3>
            <p>Now that we know who you are, I know who I am. I'm not a mistake! It all makes sense! In a comic, you know how you can tell who the arch-villain's going to be? He's the exact opposite of the hero.</p>
        </div>
      </div>
    </about>

1 个答案:

答案 0 :(得分:0)

您可以将其opacity设置为0transform:transationY(100%)并创建一个名为show或具有opacity:1;transform:translateY(0);属性的类

然后您将需要此 JS 代码

    var columns = document.getElementsByClassName("col");
    document.addEventListener("scroll",function(){
        for(var i = 0; i < columns.length; i++){
           if(columns[i].getBoundingClientRect().top < window.innerHeight){
              columns[i].classList.add("show");
           }
        }
    });
.col{
width:100%;
height:300px;
background:#555;
border:1px solid #000;
margin-bottom:50px;
opacity:0;
transform:translateY(100%);
transition:opacity .5s, transform .5s;
}

.show{
opacity:1;
transform:translateY(0);
}
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>

其中columns[i].getBoundingClientRect().top < window.innerHeight检查div在滚动时是否显示在窗口中