不理解为什么一直提示联系.html:23 Uncaught TypeError: Cannot set property 'onmouseover' of nul ,检查了好多遍,不懂问题出在那块儿,哪位大神帮忙看下,谢谢l

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

control123

2016-09-23 15:42

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>
dbhs
    </title>
<style type="text/css">
div{
    width:200px;
    height:100px;
    background-color:yellow;
    border:2px solid #000;
    opacity: 0.3;
    filter:alpha(opacity:30);
}
</style>

<script type="text/javascript">

window.onload=function(){
    var odiv =document.getElementById("div");
  odiv.onmouseover=function(){
    startMove(odiv,'width',400);
  }
}

function startMove(obj,attr,iTarget,fn){
  clearInterval(obj.timer);
  obj.timer=setInterval(function(){
    var icur=null;
    if(attr=='opacity'){
      icur=Math.round(parseFloat(getStyle(obj,attr))*100);
    }else{
      icur=parseInt(getStyle(obj,attr));
    }
    var speed=(iTarget-icur)/10;
    speed=speed>0?Math.ceil(speed):Math.floor(speed);
    if(icur==iTarget){
      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>
</script>
</head>
<body>
<div id="div1">

</div>
</body>
</html>

写回答 关注

3回答

  • Cancer_bin
    2016-10-18 09:53:53

    你的id获取错了var odiv =document.getElementById("div");

    改成var odiv =document.getElementById("div1");

  • 慕前端8664132
    2016-09-30 00:20:33

    我没有这水平,只能围观了

  • 哇妙012
    2016-09-23 17:15:03

    往上数倒数第七行多写了一个</script>结束标签,你看是不是这个原因

JS动画效果

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

113920 学习 · 1502 问题

查看课程

相似问题