且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CSS3-动画在视口中,如果可见元素(页面滚动)

更新时间:2023-01-26 11:37:21

您需要使用 JS 的jQuery 触发 CSS3 过渡/动画结果
一旦元素在视口中**。

You'll need to use JS or jQuery to trigger your CSS3 transition / animation
once the element is in viewport**.

的jsfiddle演示 - 使用inViewport插件

聆听负荷调整滚动事件得到的如果一个元素进入视窗的。结果
您可以使用小的jQuery插件我已经建立:
(http://***.com/a/26831113/383904)

listening to load, resize and scroll events to get if an element entered the Viewport.
You can use a small jQuery plugin I've built: (http://***.com/a/26831113/383904)

让我们假设你有一个像动画框:

Let's say you have boxes that animate like:

<div class="box rotate"></div>
<div class="box scale"></div>
<div class="box translate"></div>

比你的CSS你有:

than in your CSS you have:

.box{
    width:300px;
    height:300px;
    margin:500px 50px;
    background:red;
    transition: 1.5s; /* THE DURATION */
}

.rotate.triggeredCSS3    {transform : rotate(360deg); }
.scale.triggeredCSS3     {transform : scale(1.6); }
.translate.triggeredCSS3 {transform : translate3d(400px,0,0); }

其中 .triggeredCSS3 将由插件动态分配的:

$(".box").inViewport(function(px){
    if(px) $(this).addClass("triggeredCSS3") ;
});