功能残缺,添加opacity时候无法正常添加上,望大侠帮忙修改下,总得来位大侠来拯救小弟吧,慕课的暖风吹过来。。~小弟在此召唤 啊啊啊啊啊啊啊啊啊啊!!!

来源:6-2 完美运动框架

蛋疼少年的和谐青春

2016-01-08 15:06

<!--HTML
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #wrap{
            width: 200px;
            height: 100px;
            opacity:1;
            filter: alpha(opacity:100);
            background-color:#7affdd;
        }
    </style>
</head>
<script src="JS--Animation-10.js"></script>
<body>
    <div id="wrap"></div>
</body>
</html>

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
JavaScript
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/**
 * Created by Administrator on 2016/1/6.
 */
window.onload=function(){
    var div = document.getElementById('wrap');
    div.onmouseover=function(){
        setMove(div,{width:500,height:400,opacity:.5});
    }
    div.onmouseout=function(){
        setMove(div,{width:200,height:100});
    }
}

function setMove(obj,json,fu){
    var flas =true;
    clearInterval(obj.timer);
    obj.timer=setInterval(function(){
        for(var sty in json){
            var ico = 0;
            if(sty=='opacity'){
                ico = Math.random(parseFloat(setTmo(obj,sty))*100);
            }else{
                ico = parseInt(setTmo(obj,sty));
            }
            var speed = (json[sty] -ico)/5;
            speed = speed>0? Math.ceil(speed):Math.floor(speed);
            if(json[sty] !=ico){
                flas =false;
            }
            if(json[sty]=='opacity'){
                obj.style.filter='alpha(opacity'+(ico+speed)+"px";
                obj.style.opacity=(ico+speed)/100;
            }else{
                obj.style[sty]=ico+speed+"px";
            }
            if(flas){
                clearInterval(obj.timer);
                if(fu){
                    fu();
                }
            }
        }
    },30)
}

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


写回答 关注

2回答

  • 伊望岁月
    2016-01-08 15:32:18
    已采纳
    window.onload=function(){
        var div = document.getElementById('wrap');
        div.onmouseover=function(){
            setMove(div,{width:500,height:400,opacity:50});
        }
        div.onmouseout=function(){
            setMove(div,{width:200,height:100,opacity:100});
        }
    }
    
    function setMove(obj,json,fu){
        clearInterval(obj.timer);
        obj.timer=setInterval(function(){
            var flas =true;
            for(var sty in json){
                var ico = 0;
                if(sty=='opacity'){
                    ico = Math.round(parseFloat(setTmo(obj,sty))*100);
                }else{
                    ico = parseInt(setTmo(obj,sty));
                }
                var speed = (json[sty] -ico)/5;
                speed = speed>0? Math.ceil(speed):Math.floor(speed);
                if(json[sty] !=ico){
                    flas =false;
                }
                if(sty=='opacity'){
                    obj.style.filter='alpha(opacity:'+(ico+speed)+")";
                    console.log((ico+speed)/100);
                    obj.style.opacity=(ico+speed)/100;
                }else{
                    obj.style[sty]=ico+speed+"px";
                }
                if(flas){
                    clearInterval(obj.timer);
                    if(fu){
                        fu();
                    }
                }
            }
        },30);
    }
    
    function setTmo(obj,attr){
        if(obj.currentStyle){
            return obj.currentStyle[attr];
        }else{
            return getComputedStyle(obj,false)[attr];
        }
    }

    错误太多了,首先你的opacity的取值是(0到100)才对,

    然后ico = Math.round(parseFloat(setTmo(obj,sty))*100);你写成了Math.random,

    if(sty=='opacity'){写成了if(json[sty]=='opacity'){

    obj.style.filter='alpha(opacity:'+(ico+speed)+")";写成了obj.style.filter='alpha(opacity'+(ico+speed)+"px";


    蛋疼少年的和...

    非常感谢您这么详细的指出我的错误,谢谢您,我以后也会多多注意细节方面。辛苦啦!!嘿嘿 感谢大侠~!!!!!

    2016-01-08 15:45:06

    共 1 条回复 >

  • 产品经理不是经理
    2016-01-08 15:19:43
    <!--HTML
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    -->
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                padding: 0;
                margin: 0;
            }
            #wrap{
                width: 200px;
                height: 100px;
                opacity:1;
                filter: alpha(opacity:100);
                background-color:#7affdd;
            }
        </style>
    </head>
    <body>
        <div id="wrap"></div>
    </body>
    
     
    <script type="text/javascript">
    window.onload=function(){
        var div = document.getElementById('wrap');
        div.onmouseover=function(){
            setMove(div,{width:500,height:400,opacity:.5});
        }
        div.onmouseout=function(){
            setMove(div,{width:200,height:100});
        }
    }
     
    function setMove(obj,json,fu){
        var flas =true;
        clearInterval(obj.timer);
        obj.timer=setInterval(function(){
            for(var sty in json){
                var ico = 0;
                if(sty=='opacity'){
                    ico = Math.random(parseFloat(setTmo(obj,sty))*100);
                }else{
                    ico = parseInt(setTmo(obj,sty));
                }
                var speed = (json[sty] -ico)/5;
                speed = speed>0? Math.ceil(speed):Math.floor(speed);
                if(json[sty] !=ico){
                    flas =false;
                }
                if(json[sty]=='opacity'){
                    obj.style.filter='alpha(opacity'+(ico+speed)+"px";
                    obj.style.opacity=(ico+speed)/100;
                }else{
                    obj.style[sty]=ico+speed+"px";
                }
                if(flas){
                    clearInterval(obj.timer);
                    if(fu){
                        fu();
                    }
                }
            }
        },30)
    }
     
    function setTmo(obj,attr){
        if(obj.currentStyle){
            return obj.currentStyle[attr];
        }else{
            return getComputedStyle(obj,false)[attr];
        }
    }
    </script></html>


    蛋疼少年的和...

    鼠标移动上去后,透明度还是没有改变呢,大侠。。。。

    2016-01-08 15:32:43

    共 1 条回复 >

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113917 学习 · 1502 问题

查看课程

相似问题