【发布时间】:2018-02-02 02:56:03
【问题描述】:
目标是一旦用户滚动并看到红色框,就会触发 CSS 动画。它工作得很好,除了我期待所有的盒子开始完全隐藏,然后第一个盒子淡入,然后第二个盒子淡入,然后是第三个等等。但它们都开始可见,然后在消失前很快消失在。
如何让所有 4 个框开始不可见,然后仅在它们各自的淡入淡出动画开始时才出现?
;
(function($, win) {
$.fn.inViewport = function(cb) {
return this.each(function(i, el) {
function visPx() {
var H = $(this).height(),
r = el.getBoundingClientRect(),
t = r.top,
b = r.bottom;
return cb.call(el, Math.max(0, t > 0 ? H - t : (b < H ? b : H)));
}
visPx();
$(win).on("resize scroll", visPx);
});
};
}(jQuery, window));
$(function() { // DOM is now ready
$(".animateinview").inViewport(function(px) {
if (px) $(this).addClass("triggeredCSS3");
});
});
.space {
height: 800px;
}
.column {
height: 100px;
width: 100px;
background: red;
margin-bottom: 5px;
}
.fadeinfast.triggeredCSS3 {
-webkit-animation: fadein 1s;
/* Safari, Chrome and Opera > 12.1 */
-moz-animation: fadein 1s;
/* Firefox < 16 */
-ms-animation: fadein 1s;
/* Internet Explorer */
-o-animation: fadein 1s;
/* Opera < 12.1 */
animation: fadein 1s;
}
.fadeinfast.fadein1.triggeredCSS3 {
animation-delay: 0s;
}
.fadeinfast.fadein2.triggeredCSS3 {
animation-delay: 1s;
}
.fadeinfast.fadein3.triggeredCSS3 {
animation-delay: 2s;
}
.fadeinfast.fadein4.triggeredCSS3 {
animation-delay: 3s;
}
@keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
/* Firefox < 16 */
@-moz-keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
/* Internet Explorer */
@-ms-keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
/* Opera < 12.1 */
@-o-keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="space">
</div>
<div class="column fadeinfast fadein1 animateinview">
</div>
<div class="column fadeinfast fadein2 animateinview">
</div>
<div class="column fadeinfast fadein3 animateinview">
</div>
<div class="column fadeinfast fadein4 animateinview">
</div>
【问题讨论】: