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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容

  • JavaScript 动画框架 框架封装 相信大家在很多门户网站上都可以看到动画的交互效果,通过这些动画生动地体现...
    蝉翅的空响阅读 1,199评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,451评论 25 707
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,727评论 1 92
  • 2016.11.16 1/21 推荐语: 公关(public relations)是什么,在现在这个年代似乎这两...
    学简阅读 332评论 0 0
  • 今年和去年连续两年,互联网行业非常不景气,创业团队死了一批又一批,大公司开始实行996变相裁员,比如58,闹得沸沸...
    二哥说两句阅读 443评论 0 1