js運(yùn)動(dòng)6 完美運(yùn)動(dòng)框架

  • 實(shí)現(xiàn)同時(shí)改變對(duì)象的多種屬性變化
  • 使用了json來(lái)傳參

move.js

/**
 * Created by admin on 2017/3/20.
 */
/**
 *
 * @param obj 運(yùn)動(dòng)的對(duì)象
 * @param json {width:400, height:400}
 * @param fnEnd回調(diào)
 */
function move(obj, json, fnEnd){
    clearInterval(obj.timer);
    obj.timer = setInterval(function(){
        for(var attr in json){
            var target = json[attr];
            if(attr == "opacity")
                var cur = parseFloat(getStyle(obj, attr))*100;
            else
                var cur = parseInt(getStyle(obj, attr));

            var speed = (target - cur)/6;
            speed = speed>0 ? Math.ceil(speed) : Math.floor(speed);

            if(cur == target){
                clearInterval(obj.timer);
                console.log(obj + "---" + attr+ " to " +target+ " is over ");
                if(fnEnd)fnEnd();
            }else{
                if(attr == "opacity"){
                    obj.style.opacity = (cur + speed)/100;
                    obj.style.filter = "alpha(opacity:"+(cur+speed)+")";
                }
                else
                    obj.style[attr] = (cur + speed) + "px";
            }
        }
    }, 30)
}


/**
 * 獲取行間/內(nèi)聯(lián)/外部樣式,無(wú)法設(shè)置
 * @param obj
 * @param attr
 */
function getStyle(obj, attr){
    if(obj.currentStyle){
        return obj.currentStyle[attr];
    }else{
        return getComputedStyle(obj, false)[attr];
    }
}

HTML

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>完美運(yùn)動(dòng)框架</title>
    <script src="js/move2.js"></script>
    <style>
        body{
            background-color: #cccccc;
        }
        div{
            width: 100px;
            height: 100px;
            background-color: crimson;
            border: 2px solid white;
            margin: 10px;
            float: left;
            color: white;
            text-align: center;
            line-height: 100px;
            font-size: 15px;
            opacity: 0.3;
            filter: alpha(opacity:30);
        }
    </style>

    <script>
        window.onload = function(){
            var div = document.getElementsByTagName("div")[0];
            div.onmouseover = function(){
               move(this, {width:200, height:200, opacity:100, fontSize:30});
            }
            div.onmouseout = function(){
                move(this, {width:100, height:100, opacity:30, fontSize:15});
            }
        }
    </script>
</head>
<body>
<div>測(cè)試</div>
</body>
</html>

效果

jsSport6.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,839評(píng)論 4 61
  • 孝敬父母是不容等待的,別待父母老去了,回憶里有更多的愧疚。如果父母安在,給父母更多的陪伴。 2017年9月5日, ...
    紅利lihong閱讀 504評(píng)論 1 10
  • 經(jīng)常在五星級(jí)酒店的總統(tǒng)套房醒來(lái), 比起早餐和午餐,更愛(ài)Brunch, 接著直接High tea, 定時(shí)報(bào)到的是奢侈...
    久別Vintage閱讀 429評(píng)論 0 1
  • 01 孤獨(dú)和獨(dú)處并不同。 獨(dú)處是空間上的一個(gè)人,孤獨(dú)是心理層面的一個(gè)人。 首先我們明確一個(gè)前提吧——孤獨(dú)本身是中性...
    一木南閱讀 1,805評(píng)論 0 0

友情鏈接更多精彩內(nèi)容