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

js運(yùn)動(dòng)6 完美運(yùn)動(dòng)框架 其實(shí)有bug
看以下例子,width先結(jié)束就會(huì)關(guān)閉掉定時(shí)器

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:101, height:200, opacity:100, fontSize:30});
            }
        }
    </script>
</head>
<body>
<div>測(cè)試</div>
</body>
</html>

效果

jsSport7.gif

改move.js

加入了stop標(biāo)志位

/**
 * Created by admin on 2017/3/20.
 */
/**
 *
 * @param obj 運(yùn)動(dòng)的對(duì)象
 * @param json {width:400, height:400}
 * @param fn 回調(diào)
 */
function move(obj, json, fnEnd){
    clearInterval(obj.timer);
    obj.timer = setInterval(function(){
        var stop = true;
        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){
                stop = false;
                if(attr == "opacity"){
                    obj.style.opacity = (cur + speed)/100;
                    obj.style.filter = "alpha(opacity:"+(cur+speed)+")";
                }
                else
                    obj.style[attr] = (cur + speed) + "px";
            }
            console.log(obj + "---" + attr+ " stop " +stop );
        }
        if(stop){
            clearInterval(obj.timer);
            console.log(obj + "---" + attr+ " to " +target+ " is over ");
            if(fnEnd)fnEnd();
        }

    }, 30)
}

效果

jsSport8.gif
QQ圖片20170320143743.png
QQ圖片20170320143826.png
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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