CSS3アニメーション

CSS3 アニメーション

CSS3 を使用すると、多くの Web ページのアニメーション画像、Flash アニメーション、および JAVAScript を置き換えることができるアニメーションを作成できます。


CSS3 @keyframes ルール

CSS3 アニメーションを作成するには、@keyframes ルールを理解する必要があります。

@keyframes ルールはアニメーションを作成するためのものです。 @keyframes ルール内で CSS スタイルとアニメーションを指定し、現在のスタイルから新しいスタイルに徐々に変更します。


CSS3 アニメーション

@keyframes でアニメーションを作成するときは、それをセレクターにバインドします。そうしないと、アニメーションは効果がありません。

少なくとも次の 2 つの CSS3 アニメーション プロパティがセレクターにバインドされるように指定します:

アニメーションの名前を指定し、アニメーションの長さを指定します


「myfirst」アニメーションをバインドするdiv 要素に、Duration: 5 秒:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style> 
div
{
width:100px;
height:100px;
background:red;
animation:myfirst 5s;
-webkit-animation:myfirst 5s; /* Safari and Chrome */
}
@keyframes myfirst
{
from {background:red;}
to {background:yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */
{
from {background:red;}
to {background:yellow;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>

: アニメーションの名前とアニメーションの継続時間を定義する必要があります。期間を省略した場合、デフォルト値は 0 であるため、アニメーションは実行されません。

プログラムを実行して試してみましょう


CSS3アニメーションとは何ですか?

アニメーションは、要素をあるスタイルから別のスタイルに徐々に変更する効果です。

スタイルは何度でも好きなだけ変更できます。

変更が発生する時間を指定するにはパーセンテージを使用するか、0% と 100% に相当するキーワード「from」と「to」を使用してください。

0% はアニメーションの開始、100% はアニメーションの完了です。

ブラウザのサポートを最適化するには、常に 0% および 100% セレクターを定義する必要があります。


インスタンス

アニメーションが 25% と 50% になったときに背景色を変更し、アニメーションが 100% 完了したときに再度変更します:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style> 
div
{
width:100px;
height:100px;
background:red;
animation:myfirst 5s;
-moz-animation:myfirst 5s; /* Firefox */
-webkit-animation:myfirst 5s; /* Safari and Chrome */
-o-animation:myfirst 5s; /* Opera */
}
@keyframes myfirst
{
0%   {background:red;}
25%  {background:yellow;}
50%  {background:blue;}
100% {background:green;}
}
@-moz-keyframes myfirst /* Firefox */
{
0%   {background:red;}
25%  {background:yellow;}
50%  {background:blue;}
100% {background:green;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */
{
0%   {background:red;}
25%  {background:yellow;}
50%  {background:blue;}
100% {background:green;}
}
@-o-keyframes myfirst /* Opera */
{
0%   {background:red;}
25%  {background:yellow;}
50%  {background:blue;}
100% {background:green;}
}
</style>
</head>
<body>
<div></div>
<p><b>注释:</b>当动画完成时,会变回初始的样式。</p>
<p><b>注意:</b> 该实例在 Internet Explorer 9 及更早 IE 版本是无效的。</p>
</body>
</html>

プログラムを実行して試してみましょう


背景の色と位置を変更します:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文网(php.cn)</title>
    <style>
        div
        {
            width:100px;
            height:100px;
            background:red;
            position:relative;
            animation:myfirst 5s;
            -webkit-animation:myfirst 5s; /* Safari and Chrome */
        }
        @keyframes myfirst
        {
            0%   {background:red; left:0px; top:0px;}
            25%  {background:yellow; left:200px; top:0px;}
            50%  {background:blue; left:200px; top:200px;}
            75%  {background:green; left:0px; top:200px;}
            100% {background:red; left:0px; top:0px;}
        }
        @-webkit-keyframes myfirst /* Safari and Chrome */
        {
            0%   {background:red; left:0px; top:0px;}
            25%  {background:yellow; left:200px; top:0px;}
            50%  {background:blue; left:200px; top:200px;}
            75%  {background:green; left:0px; top:200px;}
            100% {background:red; left:0px; top:0px;}
        }
    </style>
</head>
<body>
<div>css动画</div>
</body>
</html>

プログラムを実行して試してください


CSS3 アニメーション プロパティ

次の表に、@keyframes ルールとすべてのアニメーション プロパティを示します。プロパティ

説明

CSS@keyframes アニメーションを指定します。 3animation anime-play-state プロパティを除く、すべてのアニメーション プロパティの短縮プロパティ。 3animation-name @keyframesアニメーションの名前を指定します。 3animation-duration アニメーションが 1 サイクル完了するのにかかる秒数またはミリ秒数を指定します。デフォルトは 0 です。 3animation-timing-function アニメーションのスピードカーブを指定します。デフォルトは「簡単」です。 3animation-delay アニメーションの開始時期を指定します。デフォルトは 0 です。 3animation-iteration-countは、アニメーションが再生される回数を指定します。デフォルトは 1 です。 3animation-direction 次のサイクルでアニメーションを逆再生するかどうかを指定します。デフォルトは「通常」です。 3animation-play-state アニメーションが実行中か一時停止中かを指定します。デフォルトは「実行中」です。 学び続ける
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> div { width:100px; height:100px; background:red; position:relative; animation:myfirst 5s; -webkit-animation:myfirst 5s; /* Safari and Chrome */ } @keyframes myfirst { 0% {background:red; left:0px; top:0px;} 25% {background:yellow; left:200px; top:0px;} 50% {background:blue; left:200px; top:200px;} 75% {background:green; left:0px; top:200px;} 100% {background:red; left:0px; top:0px;} } @-webkit-keyframes myfirst /* Safari and Chrome */ { 0% {background:red; left:0px; top:0px;} 25% {background:yellow; left:200px; top:0px;} 50% {background:blue; left:200px; top:200px;} 75% {background:green; left:0px; top:200px;} 100% {background:red; left:0px; top:0px;} } </style> </head> <body> <div>css动画</div> </body> </html>
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!
3 結果: