CSS3 animation and keyframes關(guān)鍵幀

關(guān)鍵幀--keyframes

  • 類似于flash

  • 只需指名兩個(gè)狀態(tài),之間的過程由計(jì)算機(jī)自動(dòng)計(jì)算

  • 關(guān)鍵幀的時(shí)間單位

    • 數(shù)字: 0% 25% 100%等(不寫0%或100%的狀態(tài),就默認(rèn)為已經(jīng)設(shè)置的樣式)
    • 字符: from(0%) , to(100%)
  • 格式(1)

    • @keyframes 動(dòng)畫名稱{動(dòng)畫狀態(tài)}
  • 格式 (2)

  • @keyframes move{from{background:red;} to {background:green};}

  • 可以只有to

animation基本使用

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <title>關(guān)鍵幀</title>
</head>
<style>
    *{margin: 0px;padding:0px;}
    @-webkit-keyframes move {
        0%{width:0px;}
        30%{width:50px;}
        50%{width:100px;}
        75%{width:150px;}
        100%{width:200px;}
    }
    @-moz-keyframes move{
        0%{width:0px;}
        30%{width:50px;}
        50%{width:100px;}
        75%{width:150px;}
        100%{width:200px;}
    }
    @keyframes move{
        0%{width:0px;}
        30%{width:50px;}
        50%{width:100px;}
        75%{width:150px;}
        100%{width:200px;}
    }
    .box{background:red;width:200px;height:30px;border:1px solid black;-webkit-animation:2s move linear;-moz-animation: 2s move linear;}
</style>
<body>
<div class="box"></div>
</body>
</html>

animation屬性

-webkit-animation: 動(dòng)畫時(shí)間 延遲時(shí)間 動(dòng)畫名稱 運(yùn)動(dòng)狀態(tài) 次數(shù) 播放前重置

-webkit-animation:2s 1s move linear infinite alternate

動(dòng)畫播放暫停

  • animation-play-state: 播放狀態(tài)(running播放和paused暫停)

播放前重置用的很少

  • alternate:動(dòng)畫直接從上一次挺值得位置開始執(zhí)行
  • normal :動(dòng)畫第二次直接跳到0%的狀態(tài)開始執(zhí)行

animation-Js結(jié)合

  • 通過class
    • 在class里面加入animation的各種屬性
    • 直接給元素加入 -webkit-animation-xxx樣式
  • animation的問題
    • 寫起來(lái)麻煩
    • 沒辦法改變目標(biāo)點(diǎn)的位置

obj.addEventListener("webkitAnimationEnd",function(){})

走回字和鼠標(biāo)暫停

效果圖如下:

小球運(yùn)動(dòng).gif

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-Type' content="text/html; charset=utf-8">
    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
    <title>走回字和暫停播放</title>
</head>
<style>
    *{margin:0px;padding:0px;}
    .box{width:400px;height:400px;border:1px solid black;margin:100px auto;position:relative;}
    @-webkit-keyframes move {
        25%{left:370px;top:0px;}
        50%{left:370px;top:370px;}
        75%{left:0px;top:370px;}
       100%{left:0px;top:0px;}
    }
    .box:hover .circle{-webkit-animation-play-state: paused;}
    .circle{width:30px;height:30px;background:red;border-radius: 50%;position:absolute;left:0px;top:0px;-webkit-animation: 4s 1s move infinite;}
</style>
<body>
<div class="box">
    <div class="circle"></div>
</div>
</body>
</html>

正序倒序播放

效果如下

小球運(yùn)動(dòng)正序或者倒敘.gif

代碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-Type' content="text/html; charset=utf-8">
    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
    <title>走回字和暫停播放</title>
</head>
<style>
    *{margin:0px;padding:0px;}
    .box{width:100px;height:100px;border:1px solid black;margin:100px auto;position:relative;}
    @-webkit-keyframes move {
        25%{left:70px;top:0px;}
        50%{left:70px;top:70px;}
        75%{left:0px;top:70px;}
        100%{left:0px;top:0px;}
    }
    .box:hover .circle{-webkit-animation-play-state: paused;}
    .circle{width:30px;height:30px;background:red;border-radius: 50%;position:absolute;left:0px;top:0px;-webkit-animation: 4s 1s move infinite alternate;}
</style>
<body>
<div class="box">
    <div class="circle"></div>
</div>
</body>
</html>

處理完畢后回到默認(rèn)的樣式

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-Type' content="text/html; charset=utf-8">
    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
    <title>animation后面加class</title>
</head>
<style>
    *{margin:0px;padding:0px;}
    .box{width:100px;height:100px;border:1px solid black;margin:100px auto;position:relative;}
    @-webkit-keyframes move {
        0%{left:0px;top:0px;}
        25%{left:70px;top:0px;}
        50%{left:70px;top:70px;}
        75%{left:0px;top:70px;}
        100%{left:0px;top:0px;}
    }
    .circle{width:30px;height:30px;background:red;border-radius: 50%;position:absolute;left:0px;top:0px;}
    .move{-webkit-animation: 4s 1s move ; /*最后的left*/left:70px;}
    p{font-size:23px;text-align:center;}
</style>
<body>
<p>通過類名加動(dòng)畫效果</p>
<div class="box">
    <div class="circle move"></div>
</div>
</body>
</html>

animationed事件

效果如下

animationEnd.gif
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>配合animationEnd</title>
    <style>
        @-webkit-keyframes move {
            0% {
                width: 100px;
            }
            100% {
                width: 500px;
            }
        }
        @-moz-keyframes move {
            0% {
                width: 100px;
            }
            100% {
                width: 500px;
            }
        }
        @keyframes move {
            0% {
                width: 100px;
            }
            100% {
                width: 500px;
            }
        }
        .box {width: 100px; height: 100px; background: red;}
        .move {-webkit-animation: 2s move; -moz-animation: 2s move; animation: 2s move; width: 500px;}
        p{text-align:center;}
    </style>
</head>
<body>
<p>點(diǎn)擊后配合animationEnd事件點(diǎn)擊后就會(huì)變化</p>
<div class="box" id="box"></div>
<script>
    document.getElementById('box').onclick = function(){
        this.className = 'box move';
        addEnd(this, fn);
    }

    function fn(){
        alert('end');
    }

    function addEnd(obj, fn){
        obj.addEventListener('webkitAnimationEnd', fn, false);
        obj.addEventListener('animationend', fn, false);
    }
</script>
</body>
</html>

簡(jiǎn)易版本的無(wú)縫滾動(dòng)

效果圖

無(wú)縫滾動(dòng).gif

代碼如下

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv='Content-Type' content="text/html; charset=utf-8">
    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
    <title>簡(jiǎn)易版本的無(wú)縫滾動(dòng)</title>
</head>
<style>
    *{margin:0px;padding:0px;}
    ul{list-style-type:none;}
    li{float:left;}
    #wrap{width:250px;height:50px;position:relative;bordr:1px solid #ccc;margin:100px auto;overflow: hidden;}
    #wrap ul{position:absolute;left:0px;top:0px;height:50px;width:500px;-webkit-animation: 3s 1s move infinite;}
    #wrap ul li {width:50px;height:50px;font:24px/50px "微軟雅黑";background:black;color:#ffffff;text-align:center;}
    @-webkit-keyframes move {
        0%{
            left:0px;
        }
        100%{left:-250px;}/*走的距離就是盒子的寬度*/
    }
    #wrap:hover ul{-webkit-animation-play-state: paused;}
</style>
<body>
<div id="wrap">
<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
</ul>
</div>
</body>
</html>

以上CSS3 animation 知識(shí)點(diǎn)全部完畢

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市砾赔,隨后出現(xiàn)的幾起案子暴心,更是在濱河造成了極大的恐慌杂拨,老刑警劉巖弹沽,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡亏狰,警方通過查閱死者的電腦和手機(jī)役纹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)暇唾,“玉大人促脉,你說我怎么就攤上這事辰斋。” “怎么了瘸味?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵宫仗,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我旁仿,道長(zhǎng),這世上最難降的妖魔是什么枯冈? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任毅贮,我火速辦了婚禮,結(jié)果婚禮上尘奏,老公的妹妹穿的比我還像新娘滩褥。我一直安慰自己,他們只是感情好炫加,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布瑰煎。 她就那樣靜靜地躺著,像睡著了一般俗孝。 火紅的嫁衣襯著肌膚如雪酒甸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天驹针,我揣著相機(jī)與錄音烘挫,去河邊找鬼。 笑死柬甥,一個(gè)胖子當(dāng)著我的面吹牛饮六,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播苛蒲,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼卤橄,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了臂外?” 一聲冷哼從身側(cè)響起窟扑,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎漏健,沒想到半個(gè)月后嚎货,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蔫浆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年殖属,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瓦盛。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡洗显,死狀恐怖外潜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情挠唆,我是刑警寧澤处窥,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站玄组,受9級(jí)特大地震影響滔驾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜巧勤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一嵌灰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧颅悉,春花似錦沽瞭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至延曙,卻和暖如春豌鹤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背枝缔。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工布疙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人愿卸。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓灵临,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親趴荸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子儒溉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容