您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

animation 动画

animation 是动画,而 transition 是过渡,它们很相似,但实际又不大相同,可以说 animationtransition 的版,它可以创建持续的执行动画。

animation 是简写,它是下面的缩写:

animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation-fill-mode

注意:请始终规定 animation-duration ,否则时长为 0,就不会播放动画了。

animation 是几个值的缩写,我们定义动画通常使用 animation 就足够了,如果想单独改变动画的某些可以使用单独的动画去改变,构成动画的最基本需要 @keyframesduration

.demo{
    animation: name duration timing-function delay iteration-count direction;
}

值说明:

animation-direction 参数值详解:

animation-fill-mode 参数值详解

 @keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ;
}

说明:这是最简单的动画创建方式,使用 @keyframes 命名叫做 go 的动画,再为这个动画 2s 的持续时间 ,构成了最简单的动画,但是它只播放一次。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in;/*这里了动画*/
}

说明:通过在 animation 第 3 个参数 animation-timing-function 动画,它可以改变动画@H_265_@运动的速度曲线。

Tips:要注意的是,不管怎么改变动画的结束时间是不会变的。具体可以看 timing-function 的介绍。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s;/*这里了动画延迟时间 3 秒*/
}

说明:动画延迟了 3 秒开始再次执行了。

动画延迟 3s 开始播放,播放 2 次结束。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s ;/*播放 2 次结束*/
}

说明:通过图可以很清楚的看到了动画反复执行了 2 次,@H_265_@值得注意的这个 3s 的延迟只针对第一次动画开始前,在动画开始之后重复循环的时候就不再起作用了。

动画延迟 3s 开始播放,然后无限循环。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s infinite;/*无限次循环*/
}

说明:通过 infinite 动画在经过 3s 的延迟之后开始无限的循环了。

延续上面的例子,我们发现动画每次循环都是从开始的位置开始循环的,下面通过 animation-direction 来改变动画在循环过程中是否反向。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s infinite reverse;/*动画反向播放*/
}

使用 alternate ,让动画在奇数时候正向播放,偶数时候反向播放。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s infinite alternate;
}

<!DOCTYPE html>
<html lang="en">
<head>
    < charset="UTF-8">
    < name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @keyframes go{
            from{
                width:px;
            }
            to{
                width:px
            }
        }
        .demo{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s infinite;/*动画反向播放*/
        }
        .demo-1{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s infinite reverse;/*动画反向播放*/
        }
        .demo-2{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s infinite alternate;/*动画偶数反向播放*/
        }
    </style>
</head>
<body>
    <h2>正常播放顺序</h2>
    <div class="demo"></div>
    <h2>反向播放顺序</h2>
    <div class="demo-1"></div>
    <h2>奇数正向播放偶数次反向播放</h2>
    <div class="demo-2"></div>
</body>
</html>

单次动画使用 forwards 设置动画结束之后使用结束后的状态作为样式。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s  forwards;
}

在设置延迟之后元素中使用 backwards 设置动画的开始的样式。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s  backwards;
}

<!DOCTYPE html>
<html lang="en">
<head>
    < charset="UTF-8">
    < name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @keyframes go{
            from{
                width:px;
            }
            to{
                width:px
            }
        }
        .demo{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s ;/*动画反向播放*/
        }
        
        .demo-1{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s  forwards;
        }
        .demo-2{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s  backwards;
        }
    </style>
</head>
<body>
    <h2>正常动画</h2>
    <div class="demo"></div>
    <h2>设置 forwards</h2>
    <div class="demo-1"></div>
    <h2>设置 backwards 注意观察开始</h2>
    <div class="demo-2"></div>
</body>
</html>

说明:在鼠标刷新浏览器我们看到本应该 100px 宽度的元素是以 200px 开始的,当动画结束之后,回到了 100px。

both 在设置动画延迟情况下,元素使用开始的状态,并在整个动画结束之后使用结束之后的状态。

@keyframes go{
    from{
        width:px;
    }
    to{
        width:px
    }
}
.demo{
    width:px;
    height:px;
    background: #000;
    animation:go s ease-in s  both;
}

<!DOCTYPE html>
<html lang="en">
<head>
    < charset="UTF-8">
    < name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @keyframes go{
            from{
                width:px;
            }
            to{
                width:px
            }
        }
        .demo{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s ;/*动画反向播放*/
        }
        .demo-3{
            width:px;
            height:px;
            background: #000;
            animation:go s ease-in s  both;
        }
    </style>
</head>
<body>
    <h2>正常动画</h2>
    <div class="demo"></div>
    <h2>设置 both 注意观察开始和结束</h2>
    <div class="demo-3"></div>
</body>
</html>

当动画造成的卡顿,可以用下面这种方式尝试:
开启 GPU 加速,例如使用 transform:transition3d(0,0,0)

有时候需要实现鼠标 hover 到元素上,会出现。如果使用 transition 过渡发现总是不能实现这个,而 animation 是可以的:

<div class="demo">
    往事不要再提<span>人生已多风雨</span>
</div>
.demo{
    cursor: pointer;
}
.demo>span{             
    display: none;  
}
.demo:hover>span{
    display: block;
    animation: shows s forwards;
}
@keyframes shows {
    from{
        opacity: ;
    }to{
        opacity: ;
    }
}

说明: transition 不能实现(隐藏/)之过渡效,原因是 diaplay:none 设置之后虽然元素在中,但是这个不在浏览器的渲染进程里面。如果这个元素为 display:block 相当于元素从新渲染出来,这时里面的 opacity: 0 到 1 就不起作用了。所以这里使用 animation 正好可以弥补这个问题。


联系我
置顶