javascript - 是否可以在滚动时为 flexbox 元素设置动画?

标签 javascript css flexbox css-transitions

是否可以在滚动时为 flex 元素设置动画?

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

使用下面的元素(在 flexbox 中设置样式),整个“行”或单个“列”。

我还没有找到关于 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>

最佳答案

你可以设置他们的opacity0transform: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 是否滚动时是否显示在窗口中

关于javascript - 是否可以在滚动时为 flexbox 元素设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677442/

相关文章:

javascript - 如何使用 jQuery 客户端上传图片并将其添加到 div?

javascript - 如何垂直书写/显示文本?

html - float 在绝对元素上

html - 悬停转换 CSS 中的抖动

javascript - 使 Ramda.js 函数全局可访问(无需 R.)

javascript - 值始终在 console.log 中可见,但有时在输入字段中不可见

css - 我的(宽)页面在低分辨率下看起来很奇怪

html - 如何使用 Flex 水平和垂直对齐 div 中的元素

javascript - 网格元素在一列而不是一行 Material-UI

javascript - 日期选择器默认日期应该是昨天,并且始终从月份第一天开始