JS動畫效果

JS速度動畫

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS速度動畫</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        #div1 {
            width: 200px;
            height: 200px;
            background: red;
            position: relative;
            left: -200px;
        }
        #div1 span {
            width: 20px;
            height: 50px;
            background: blue;
            position: absolute;
            left: 200px;
            top: 75px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var odiv = document.getElementById("div1");
            odiv.onmouseover = function() {
                starmove(0);
            };
            odiv.onmouseout = function() {
                starmove(-200);
            };
        };
        var timer = null;

        function starmove(itarget) {
            clearInterval(timer);
            var odiv = document.getElementById("div1");
            timer = setInterval(function() {
                var speed = 0;
                if (odiv.offsetLeft < itarget) {
                    speed = 10;
                } else {
                    speed = -10;
                }
                if (odiv.offsetLeft == itarget) {
                    clearInterval(timer)
                } else {
                    odiv.style.left = odiv.offsetLeft + speed + 'px';
                }
            }, 30)
        }

    </script>
</head>
<body>
    <div id="div1">
        <span id="share">分享</span>
    </div>
</body>
</html>

JS透明度動畫

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS速度動畫</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        #div1 {
            width: 200px;
            height: 200px;
            background: red;
            filter: alpha(opacity:30);
            opacity: 0.3;
        }
        #div1 span {
            width: 20px;
            height: 50px;
            background: blue;
            position: absolute;
            left: 200px;
            top: 75px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var odiv = document.getElementById("div1");
            odiv.onmouseover = function() {
                starmove(100);
            };
            odiv.onmouseout = function() {
                starmove(30);
            };
        };

        var timer = null;
        var alpha = 30;

        function starmove(itarget) {
            var odiv = document.getElementById("div1");
            clearInterval(timer);
            timer = setInterval(function() {
                var speed = 0;
                if (alpha > itarget) {
                    speed = -10;
                } else {
                    speed = 10;
                }
                if (alpha == itarget) {
                    clearInterval(timer);
                } else {
                    alpha += speed;
                    odiv.style.filter = 'alpha(opacity:' + alpha + ')';
                    odiv.style.opacity = alpha / 100;
                }
            }, 30)
        }

    </script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

JS緩沖動畫

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS速度動畫</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        #div1 {
            width: 200px;
            height: 200px;
            background: red;
            position: relative;
            left: -200px;
        }
        #div1 span {
            width: 20px;
            height: 50px;
            background: blue;
            position: absolute;
            left: 200px;
            top: 75px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var odiv = document.getElementById("div1");
            odiv.onmouseover = function() {
                starmove(0);
            };
            odiv.onmouseout = function() {
                starmove(-200);
            };
        };
        var timer = null;

        function starmove(itarget) {
            clearInterval(timer);
            var odiv = document.getElementById("div1");
            timer = setInterval(function() {
                var speed = (itarget - odiv.offsetLeft) / 10;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if (odiv.offsetLeft == itarget) {
                    clearInterval(timer)
                } else {
                    odiv.style.left = odiv.offsetLeft + speed + 'px';
                }
            }, 30)
        }

    </script>
</head>
<body>
    <div id="div1">
        <span id="share">分享</span>
    </div>
</body>
</html>

JS多物體動畫

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS多物體動畫</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        ul,li{
            list-style: none;
        }
        ul li{
            width: 200px;
            height: 100px;
            background: yellow;
            margin-bottom: 20px;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var ali = document.getElementsByTagName("li");
            for (var i = 0; i < ali.length; i++) {
                ali[i].zzz = null
                ali[i].onmouseover = function(e) {
                    starmove(this, 800);
                }
                ali[i].onmouseout = function(e) {
                    starmove(this, 200);
                }
            }
        };

        function starmove(obj, itarget) {
            clearInterval(obj.zzz);
            var odiv = document.getElementById("div1");
            obj.zzz = setInterval(function() {
                var speed = (itarget - obj.offsetWidth) / 8;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if (obj.offsetWidth == itarget) {
                    clearInterval(obj.zzz)
                } else {
                    obj.style.width = obj.offsetWidth + speed + 'px';
                }
            }, 30)
        }

    </script>
</head>
<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
</html>

JS透明度多物體動畫

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS透明度多物體動畫</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        div {
            width: 200px;
            height: 200px;
            background: red;
            filter: alpha(opacity:30);
            opacity: 0.3;
            margin: 10px;
            float: left;
        }

    </style>
    <script type="text/javascript">
        window.onload = function() {
            var odiv = document.getElementsByTagName("div");
            for (var i = 0; i < odiv.length; i++) {
                odiv[i].timer = null;
                odiv[i].alpha = 30;
                odiv[i].onmouseover = function() {
                    starmove(this, 100);
                };
                odiv[i].onmouseout = function() {
                    starmove(this, 30);
                };
            }
        };



        function starmove(obj, itarget) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function() {
                var speed = 0;
                if (obj.alpha > itarget) {
                    speed = -10;
                } else {
                    speed = 10;
                }
                if (obj.alpha == itarget) {
                    clearInterval(obj.timer);
                } else {
                    obj.alpha += speed;
                    obj.style.filter = 'alpha(opacity:' + obj.alpha + ')';
                    obj.style.opacity = obj.alpha / 100;
                }
            }, 30)
        }

    </script>
</head>
<body>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</body>
</html>

獲取樣式屬性值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>獲取樣式</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        #div1 {
            width: 200px;
            height: 200px;
            background: red;
            border: 1px solid #000;
        }

    </style>
    <script type="text/javascript">
        window.onload = function() {
            var odiv = document.getElementById("div1");
            console.log(getStyle(odiv,'background-color'));
            odiv.onmouseover = function() {
                starmove();
            };

        };


        function starmove() {

            var odiv = document.getElementById("div1");
            setInterval(function() {
                odiv.style.width=parseInt(getStyle(odiv,'width'))-1+'px';
            }, 30)
        }
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return getComputedStyle(obj,false)[attr];
            }
        }

    </script>
</head>
<body>
    <div id="div1">

    </div>
</body>
</html>

任意屬性值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>任意值屬性1</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        ul,li{
            list-style: none;
        }
        ul li{
            width: 200px;
            height: 100px;
            background: yellow;
            margin-bottom: 20px;
            border: solid 3px #000;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var li1 = document.getElementById("li1");
            var li2 = document.getElementById("li2");

            li1.onmouseover = function() {
                starmove(this, 'height', 400)
            }
            li1.onmouseout = function() {
                starmove(this, 'height', 100)
            }
            li2.onmouseover = function() {
                starmove(this, 'width', 400)
            }
            li2.onmouseout = function() {
                starmove(this, 'width', 200)
            }

        };

        function starmove(obj, attr, itarget) {
            clearInterval(obj.zzz);
            obj.zzz = setInterval(function() {
                var speed = (itarget - parseInt(getStyle(obj, attr))) / 8;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if (parseInt(getStyle(obj, 'height')) == itarget) {
                    clearInterval(obj.zzz)
                } else {
                    obj.style[attr] = parseInt(getStyle(obj, attr)) + speed + 'px';
                }
            }, 30)
        }

        function getStyle(obj, attr) {
            if (obj.currentStyle) {
                return obj.currentStyle[attr];
            } else {
                return getComputedStyle(obj, false)[attr];
            }
        }

    </script>
</head>
<body>
    <ul>
        <li id="li1"></li>
        <li id="li2"></li>
    </ul>
</body>
</html>

任意屬性值(二)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>任意值屬性1</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        ul,li{
            list-style: none;
        }
        ul li{
            width: 200px;
            height: 100px;
            background: yellow;
            margin-bottom: 20px;
            border: solid 3px #000;
            filter: alpha(opacity:30);
            opacity: 0.3;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var li1 = document.getElementById("li1");

            li1.onmouseover = function() {
                starmove(this, 'opacity', 100)
            }
            li1.onmouseout = function() {
                starmove(this, 'opacity', 30)
            }

        };

        function starmove(obj, attr, itarget) {
            clearInterval(obj.zzz);
            obj.zzz = setInterval(function() {
                var icur=0;
                if(attr=='opacity'){
                    icur=Math.round(parseFloat(getStyle(obj, attr))*100);
                }else{
                    icur=parseInt(getStyle(obj, attr));
                }

                var speed = (itarget - icur) / 8;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if (icur == itarget) {
                    clearInterval(obj.zzz)
                } else {
                    if(attr=='opacity'){
                        icur+=speed;
                        obj.style.filter = 'alpha(opacity:' + icur + ')';
                        obj.style.opacity = icur / 100;
                    }else{
                        obj.style[attr] = icur + speed + 'px';
                    }
                }
            }, 30)
        }

        function getStyle(obj, attr) {
            if (obj.currentStyle) {
                return obj.currentStyle[attr];
            } else {
                return getComputedStyle(obj, false)[attr];
            }
        }

    </script>
</head>
<body>
    <ul>
        <li id="li1"></li>
    </ul>
</body>
</html>

JS鏈式動畫

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS鏈式動畫</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        ul,li{
            list-style: none;
        }
        ul li{
            width: 200px;
            height: 100px;
            background: yellow;
            margin-bottom: 20px;
            border: solid 3px #000;
            filter: alpha(opacity:30);
            opacity: 0.3;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var li1 = document.getElementById("li1");

            li1.onmouseover = function() {
                starmove(li1, 'opacity', 100,function(){
                    starmove(li1, 'height', 500)
                });
            }
            li1.onmouseout = function() {
                starmove(this, 'height', 100,function(){
                    starmove(li1, 'opacity', 30)
                })
            }

        };

        function starmove(obj, attr, itarget,fn) {
            clearInterval(obj.zzz);
            obj.zzz = setInterval(function() {
                var icur=0;
                if(attr=='opacity'){
                    icur=Math.round(parseFloat(getStyle(obj, attr))*100);
                }else{
                    icur=parseInt(getStyle(obj, attr));
                }

                var speed = (itarget - icur) / 8;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if (icur == itarget) {
                    clearInterval(obj.zzz);
                    if(fn){
                        fn();
                    }
                } else {
                    if(attr=='opacity'){
                        icur+=speed;
                        obj.style.filter = 'alpha(opacity:' + icur + ')';
                        obj.style.opacity = icur / 100;
                    }else{
                        obj.style[attr] = icur + speed + 'px';
                    }
                }
            }, 30)
        }

        function getStyle(obj, attr) {
            if (obj.currentStyle) {
                return obj.currentStyle[attr];
            } else {
                return getComputedStyle(obj, false)[attr];
            }
        }

    </script>
</head>
<body>
    <ul>
        <li id="li1"></li>
    </ul>
</body>
</html>

完美運動框架

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>同時運動</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        ul,li{
            list-style: none;
        }
        ul li{
            width: 200px;
            height: 100px;
            background: yellow;
            margin-bottom: 20px;
            border: solid 3px #000;
            filter: alpha(opacity:30);
            opacity: 0.3;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var li1 = document.getElementById("li1");

            li1.onmouseover = function() {
                starmove(li1, {
                    width: 400,
                    height: 200,
                    opacity: 100
                });
            }
            li1.onmouseout = function() {
                starmove(li1, {
                    width: 200,
                    height: 100,
                    opacity: 30
                })
            }

        };

        function starmove(obj, json, fn) {
            var flag = true;
            clearInterval(obj.zzz);
            obj.zzz = setInterval(function() {
                for (var attr in json) {
                    var icur = 0;
                    if (attr == 'opacity') {
                        icur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
                    } else {
                        icur = parseInt(getStyle(obj, attr));
                    }

                    var speed = (json[attr] - icur) / 8;
                    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                    if (icur != json[attr]) {
                        flag = false;
                    }
                    if (attr == 'opacity') {
                        icur += speed;
                        obj.style.filter = 'alpha(opacity:' + icur + ')';
                        obj.style.opacity = icur / 100;
                    } else {
                        obj.style[attr] = icur + speed + 'px';
                    }
                    if (flag) {
                        clearInterval(obj.zzz);
                        if (fn) {
                            fn();
                        }
                    }
                }
            }, 30)
        }

        function getStyle(obj, attr) {
            if (obj.currentStyle) {
                return obj.currentStyle[attr];
            } else {
                return getComputedStyle(obj, false)[attr];
            }
        }
    </script>
</head>
<body>
    <ul>
        <li id="li1"></li>
    </ul>
</body>
</html>

d

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末草姻,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子诱担,更是在濱河造成了極大的恐慌省有,老刑警劉巖蚣常,帶你破解...
    沈念sama閱讀 211,290評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件沐祷,死亡現(xiàn)場離奇詭異钞螟,居然都是意外死亡悔醋,警方通過查閱死者的電腦和手機拢锹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評論 2 385
  • 文/潘曉璐 我一進店門谣妻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人卒稳,你說我怎么就攤上這事蹋半。” “怎么了充坑?”我有些...
    開封第一講書人閱讀 156,872評論 0 347
  • 文/不壞的土叔 我叫張陵减江,是天一觀的道長。 經(jīng)常有香客問我捻爷,道長辈灼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,415評論 1 283
  • 正文 為了忘掉前任也榄,我火速辦了婚禮戈次,結果婚禮上,老公的妹妹穿的比我還像新娘例衍。我一直安慰自己,他們只是感情好骂远,可當我...
    茶點故事閱讀 65,453評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著腰根,像睡著了一般激才。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上唠雕,一...
    開封第一講書人閱讀 49,784評論 1 290
  • 那天,我揣著相機與錄音吨述,去河邊找鬼岩睁。 笑死,一個胖子當著我的面吹牛揣云,可吹牛的內(nèi)容都是我干的捕儒。 我是一名探鬼主播,決...
    沈念sama閱讀 38,927評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼邓夕,長吁一口氣:“原來是場噩夢啊……” “哼刘莹!你這毒婦竟也來了?” 一聲冷哼從身側響起焚刚,我...
    開封第一講書人閱讀 37,691評論 0 266
  • 序言:老撾萬榮一對情侶失蹤点弯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后矿咕,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抢肛,經(jīng)...
    沈念sama閱讀 44,137評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,472評論 2 326
  • 正文 我和宋清朗相戀三年碳柱,在試婚紗的時候發(fā)現(xiàn)自己被綠了捡絮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,622評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡莲镣,死狀恐怖福稳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瑞侮,我是刑警寧澤的圆,帶...
    沈念sama閱讀 34,289評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站半火,受9級特大地震影響略板,放射性物質發(fā)生泄漏。R本人自食惡果不足惜慈缔,卻給世界環(huán)境...
    茶點故事閱讀 39,887評論 3 312
  • 文/蒙蒙 一叮称、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦瓤檐、人聲如沸赂韵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽祭示。三九已至,卻和暖如春谴古,著一層夾襖步出監(jiān)牢的瞬間质涛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工掰担, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留汇陆,地道東北人。 一個月前我還...
    沈念sama閱讀 46,316評論 2 360
  • 正文 我出身青樓带饱,卻偏偏與公主長得像毡代,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子勺疼,可洞房花燭夜當晚...
    茶點故事閱讀 43,490評論 2 348

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

  • JavaScript 動畫框架 框架封裝 相信大家在很多門戶網(wǎng)站上都可以看到動畫的交互效果教寂,通過這些動畫生動地體現(xiàn)...
    蟬翅的空響閱讀 1,212評論 0 1
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,756評論 25 707
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,737評論 1 92
  • 2016.11.16 1/21 推薦語: 公關(public relations)是什么执庐,在現(xiàn)在這個年代似乎這兩...
    學簡閱讀 332評論 0 0
  • 今年和去年連續(xù)兩年酪耕,互聯(lián)網(wǎng)行業(yè)非常不景氣,創(chuàng)業(yè)團隊死了一批又一批轨淌,大公司開始實行996變相裁員因妇,比如58,鬧得沸沸...
    二哥說兩句閱讀 445評論 0 1