13_JS緩動動畫

三個取整函數

這三個函數都是數學函數, Math

  • Math.ceil() 向上取整 天花板
    console.log(Math.ceil(1.01)) 結果 是 2
    console.log(Math.ceil(1.9)) 結果 2
    console.log(Math.ceil(-1.3)) 結果 是 -1
  • Math.floor() 向下取整 地板
    console.log(Math.floor(1.01)) 結果 是 1
    console.log(Math.floor(1.9)) 結果 1
    console.log(Math.floor(-1.3)) 結果 是 -2
  • Math.round() 四舍五入函數
    console.log(Math.round(1.01)) 結果 是 1
    console.log(Math.round(1.9)) 結果 是 2

緩動動畫

勻速動畫的原理: 盒子本身的位置 + 步長
緩動動畫的原理: 盒子本身的位置 + 步長 (不斷變化的)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
        }
    </style>
    <script type="text/javascript">
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,400);
            }
            $("btn800").onclick = function () {
                animate(box,800);
            }
        }
        function animate(obj,target){
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                var step = (target - obj.offsetLeft)/10;
                step = step>0?Math.ceil(step):Math.floor(step);
                obj.style.left = obj.offsetLeft+step+"px";
                if(obj.offsetLeft == target){
                    clearInterval(obj.timer);
                }
            },20);
        }
    </script>
</head>
<body>
<button id="btn400">400</button>
<button id="btn800">800</button>
<div id="box"></div>
</body>
</html>

JS常用訪問 CSS 屬性

我們訪問得到css 屬性,比較常用的有兩種:

  • 利用點語法

box.style.width
box.style.top

如上點語法可以得到width屬性和top屬性,帶有單位的,如:100px
但是這個語法有非常大的缺陷, 不變的。 后面的widthtop沒有辦法傳遞參數的。
不能這樣寫:var w = width;box.style.w

  • 利用 [] 訪問屬性

box.style["left"]);
元素.style[“屬性”];

語法格式: box.style[“width”]
最大的優點 : 可以給屬性傳遞參數

得到css 樣式

我們想要獲得css 的樣式, box.style.leftbox.style.backgorundColor
但是它只能得到行內的樣式。 但是我們工作最多用的是內嵌式或者外鏈式
怎么辦? 核心: 我們怎么才能得到內嵌或者外鏈的樣式呢?

  • obj.currentStyle IE、opera 常用
    外部(使用<link>)和內嵌(使用<style>)樣式表中的樣式(IE和opera常用)
  • window.getComputedStyle("元素", "偽類") w3c
    兩個選項是必須的, 沒有偽類 用 null 替代
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            background-color: pink;
            position: absolute;
            left: 10px;
            top: 0;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script type="text/javascript">
        var box = document.getElementById("box");
        //IE和Opera支持
        //IE所有版本中:100px,chrome中報錯
        console.log(box.currentStyle["width"]);
        //W3C支持的
        //chrome和IE9+中:10px,IE6、7、8中報錯
        console.log(window.getComputedStyle(box,null)["left"]);
    </script>
</body>
</html>
  • 兼容寫法 :
    我們這個元素里面的屬性很多, left top width ===
    我們想要某個屬性, 就應該 返回改屬性,所有繼續封裝返回當前樣式的 函數。
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            background-color: pink;
            position: absolute;
            left: 20px;
            top: 10px;
            z-index: 2;
            opacity: 0.4;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script type="text/javascript">
        var box = document.getElementById("box");
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        console.log(getStyle(box,"width"));//100px
        console.log(getStyle(box,"background-color"));//IE中:pink,chrome中:rgb(255, 192, 203)
        console.log(getStyle(box,"position"));//absolute
        console.log(getStyle(box,"opacity"));//0.4
        console.log(getStyle(box,"left"));//20px
        console.log(getStyle(box,"z-index"));2
    </script>
</body>
</html>

例:運動框架的基本函數(單個屬性)的封裝

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
            top: 60px;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,"height",600);
            }
            $("btn800").onclick = function () {
                animate(box,"left",400);
            }
        }
        function animate(obj, attr, target){
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                var current = parseInt(getStyle(obj,attr));
                var step = (target - current)/10;
                step = step>0?Math.ceil(step):Math.floor(step);
                obj.style[attr] = current+step+"px";
                if(current == target){
                    clearInterval(obj.timer);
                }
            },20)
        }
    </script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>

JSON遍歷:for in遍歷

for in 關鍵字

for (變量 in 對象)
{ 執行語句; }

例:運動框架的基本函數(多個屬性)的封裝

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
            top: 60px;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,{left:200,top:200});
            }
            $("btn800").onclick = function () {
                animate(box,{width:200,left:200,top:200});
            }
            function animate(obj , json){
                clearInterval(obj.timer);
                obj.timer = setInterval(function () {
                    /*
                        關閉定時器的思路,不用判斷多個屬性都到達target,
                        只要判斷有一個屬性未到target,定時器就不能停止
                     */
                    //用戶判斷是否可以關閉定時器
                    var flag  = true;
                    //開始遍歷
                    for(var attr in json){
                        //當前位置
                        var current = parseInt(getStyle(obj,attr));
                        //計算步長
                        //target = json[attr]
                        var step = (json[attr] - current)/10;
                        step = step>0?Math.ceil(step):Math.floor(step);

                        obj.style[attr] = current+step+"px";
                        if(current != json[attr]){
                            flag = false;
                        }
                    }
                    if(flag){//如果flag=true,說明多個屬性都=target了
                        clearInterval(obj.timer);
                        alert("動畫完成");
                    }

                },20);
            }
        }

    </script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>

回調函數

例:仿360開機彈窗的動畫

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        .box{
            position: fixed;
            right: 0;
            bottom: 0;
        }
        img{
            display: block;
        }
        span{
            position: absolute;
            /*background-color: pink;*/
            width: 30px;
            height: 30px;
            right: 0;
            top: 0;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function animate(obj , json , fn){
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                /*
                 關閉定時器的思路,不用判斷多個屬性都到達target,
                 只要判斷有一個屬性未到target,定時器就不能停止
                 */
                //用戶判斷是否可以關閉定時器
                var flag  = true;
                //開始遍歷
                for(var attr in json){
                    //當前位置
                    var current = parseInt(getStyle(obj,attr));
                    //計算步長
                    //target = json[attr]
                    var step = (json[attr] - current)/10;
                    step = step>0?Math.ceil(step):Math.floor(step);

                    obj.style[attr] = current+step+"px";
                    if(current != json[attr]){
                        flag = false;
                    }
                }
                if(flag){//如果flag=true,說明多個屬性都=target了
                    clearInterval(obj.timer);
                    if(fn) fn();
                }

            },20);
        }
        
        window.onload = function () {
            function $(id){return document.getElementById(id);}
            var box = $("box");
            var hd = box.getElementsByTagName("div")[0];
            var bd = box.getElementsByTagName("div")[1];
            var close = $("close");
            close.onclick = function () {
                animate(bd,{height:0}, function () {
                   animate(hd.parentNode,{width:0});
                });
            }
        }
    </script>
</head>
<body>
    <div class="box" id="box">
        <span id="close"></span>
        <div class="hd">
            ![](images/t.jpg)
        </div>
        <div class="bd">
            ![](images/b.jpg)
        </div>
    </div>
</body>
</html>

in 運算符

in運算符也是一個二元運算符,但是對運算符左右兩個操作數的要求比較嚴格。in運算符要求第1個(左邊的)操作數必須是字符串類型或可以轉換為字符串類型的其他類型,而第2個(右邊的)操作數必須是數組或對象。只有第1個操作數的值是第2個操作數的屬性名,才會返回true,否則返回false

<script type="text/javascript">
        var json = {name:"李德華",age:"45"};
        if("name" in json){
            alert("YES");
        }else{
            alert("NO");
        }
    </script>

例:運動框架的函數封裝(帶透明度的)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
            top: 60px;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,{left:200,top:200,opacity:40});
            }
            $("btn800").onclick = function () {
                animate(box,{width:200,left:200,top:200});
            }
            function animate(obj , json){
                clearInterval(obj.timer);
                obj.timer = setInterval(function () {
                    /*
                        關閉定時器的思路,不用判斷多個屬性都到達target,
                        只要判斷有一個屬性未到target,定時器就不能停止
                     */
                    //用戶判斷是否可以關閉定時器
                    var flag  = true;
                    //開始遍歷
                    for(var attr in json){
                        //當前位置
                        var current;
                        if(attr == "opacity"){
                            if("opacity" in obj.style){
                                current = parseInt(getStyle(obj,attr)*100);
                            }else{//IE6、7、8
                                var alphaStr = getStyle(obj,"filter");
                                var cs = alphaStr.substring(alphaStr.lastIndexOf("=")+1 , alphaStr.lastIndexOf(")"));
                                current = isNaN(parseInt(cs))?100:parseInt(cs);
                            }

                        }else{
                            current = parseInt(getStyle(obj,attr));
                        }

                        //計算步長
                        //target = json[attr]
                        var step = (json[attr] - current)/10;
                        step = step>0?Math.ceil(step):Math.floor(step);
                        if(attr == "opacity"){//判斷用戶是否要修改opacity屬性
                            if("opacity" in obj.style){//判斷瀏覽器是否支持opacity屬性
                                obj.style.opacity = (current+step)/100;
                            }else{
                                obj.style.filter = "alpha(opacity="+(current+step)+")";
                            }
                        }else{
                            obj.style[attr] = current+step+"px";
                        }
                        if(current != json[attr]){
                            flag = false;
                        }
                    }
                    if(flag){//如果flag=true,說明多個屬性都=target了
                        clearInterval(obj.timer);
                        alert("動畫完成");
                    }

                },20);
            }
        }

    </script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>

例:手風琴效果

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;

        }
        ul,li{
            list-style: none;
        }
        .box{
            width: 1200px;
            height: 400px;
            margin: 50px auto;
            border: 1px solid red;
            overflow: hidden;
        }
        .box ul{
            width: 120%;
            height: 100%;
        }
        .box li{
            float: left;
            width: 240px;
            height: 400px;
            /*border: 1px solid red;*/
        }
    </style>
    <script type="text/javascript" src="animate.js"></script>
    <script type="text/javascript">
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var lis = $("ul").children;
            for(var i = 0;i<lis.length ; i++){
                lis[i].style.backgroundImage = "url(images/"+(i+1)+".jpg)";
                lis[i].onmouseover = function () {
                    for(var j = 0;j<lis.length ;j++){
                        animate(lis[j],{width:75});
                    }
                    animate(this,{width:900});
                }
            }
            $("ul").onmouseout = function () {
                for(var i = 0;i<lis.length ; i++){
                    animate(lis[i],{width:240});
                }
            }
        }
    </script>
</head>
<body>
    <div class="box">
        <ul id="ul">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>
</html>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,563評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,694評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,672評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,965評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,690評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,019評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,013評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,188評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,718評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,438評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,667評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,149評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,845評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,252評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,590評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,384評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內容