为什么这个函数后面运行的那个函数先运行了

来源:5-1 JS链式动画

慕桂英9545975

2016-05-10 14:24

应该宽变宽之后跳出a,可是一开始就跳出a了

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>任意属性值2</title>
    <style type="text/css">
        body,div{  padding:0;margin:0;font:normal 12px "微软雅黑"}
        div{
            width:200px;
            height:150px;
            background-color: gold;
            top:20px;
            left:20px;
            filter: alpha(opacity:30);
            opacity: 0.3;
            float:left;
            margin-left:20px
 }
    </style>
    <script type="text/javascript">
        window.onload=function() {
            var box = document.getElementById("box");
                box.alpha=30;
                box.timer=null;
                box.onmouseover = function(){startMove(this,'width',300,alert("a"))};
        };
        function startMove(obj,attr,target,fn){
            clearInterval(obj.timer);
            obj.timer=setInterval(
                    function(){
                        var icur=0;
                        if(attr=='opacity'){
                            icur=Math.round(parseFloat(getStyle(obj,attr))*100)
                        }else{
                            icur= parseInt(getStyle(obj,attr));
                        }
                        var speed=(target-icur)/8;
                        speed=speed>0?Math.ceil(speed):Math.floor(speed);
                        if(icur==target){
                            clearInterval(obj.timer);
                            if(fn){
                                fn()
                            }
                        }else{
                            if(attr=='opacity'){
                                obj.style.filter='alpha(opacity:'+(icur+speed)+')';
                                obj.style.opacity=(icur+speed)/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>
<div id="box"></div>
</body>
</html>


写回答 关注

2回答

  • 1VAN1
    2016-05-11 09:43:32
    已采纳

    alert要用function(){}包裹起来

    慕桂英954... 回复1VAN1

    好的,谢谢

    2016-05-11 16:45:21

    共 4 条回复 >

  • BBWang
    2016-05-11 09:52:05

    你这个代码好奇怪,即不像JS的外部文件

    如果是写框架,你还需要传参吧

JS动画效果

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

113925 学习 · 1443 问题

查看课程

相似问题