纯JS实现JQUERY里animate效果(效果实现整理后)

如题,自己试着用纯JS写了下面代码,实现鼠标一上去DIV宽和高的值增大的动画效果。但是一直报错,特意来向各位大神请教,和希望给点思路,如何才能更好的实现。。。。。(现在效果实现了,但是还是有些问题,就是不能随意的移开鼠标开始缩回,鼠标放回开始放大,求大神,给个思路和建议,还有就是感觉我这样写特别笨,有没更简洁的方式。。。。。)


<!DOCTYPE html>
<html>
<head>
    <title>纯JS实现animate</title>
    <meta charset = "utf-8">
    <style>
        #change {width:100px;height:100px;background-color: red;margin:100px auto;}
    </style>
</head>
<body>
   <div id = "change"></div>
</style>
<script type="text/javascript">
    function animateself (int){

        var div = document.getElementById("change");
        div.addEventListener("mouseover",function(){
            
            var timer = setInterval(function(){
                
           var wid = window.getComputedStyle(div,null).getPropertyValue("width");
           var heig = window.getComputedStyle(div,null).getPropertyValue("height");
              //alert(wid);
              heig = heig.replace("px","");
              heig = parseInt(heig);
              wid = wid.replace("px","");
              wid = parseInt(wid);
           div.style.width = (wid+1)+"px";
           div.style.height = (heig+1)+"px";
           //div.style.width = "200px"

           //alert(wid);
           if(wid>=int&&heig>=int){
               clearInterval(timer);
           }
        },1)  
     })

        div.addEventListener("mouseout",function(){
            var timer = setInterval(function(){
                
           var wid = window.getComputedStyle(div,null).getPropertyValue("width");
           var heig = window.getComputedStyle(div,null).getPropertyValue("height");
              //alert(wid);
              heig = heig.replace("px","");
              heig = parseInt(heig);
              wid = wid.replace("px","");
              wid = parseInt(wid);
           div.style.width = (wid-1)+"px";
           div.style.height = (heig-1)+"px";
           //div.style.width = "200px"

           //alert(wid);
           if(wid<=100&&heig<=100){
               clearInterval(timer);
           }
        },1)  
    })
       
    }   
     
    animateself(500);
    
</script>
</body>
</html>
阅读 5.2k
4 个回答

setInterval 是一个 timer,不停止会一直跑下去,至少在动画完成之后应该 clearInterval 吧

    function animateself(){

        var div = document.getElementById("change");
        div.addEventListener("mouseover",function(){
           var wid = div.offsetWidth;
           div.style.width =wid+5+'px';
     });
    }

你的SetInterval用意是什么,我不清楚欸,如果用了,你记得清除时间不然会一直循环下去

var wid = window.getComputedStyle(div,null).getPropertyValue("width");
wid = wid.replace('px','');
wid = parseInt(wid);

如果你要用getComputedStyle的话

function animateself() {
        var div = document.getElementById("change");
        div.addEventListener("mouseover", function() {
            setTimeout(function() {
                var wid = window.getComputedStyle(div, null).getPropertyValue("width");
                alert("1111");
                div.style.width = (parseInt(wid.toString()) + 5).toString() + 'px';
            }, 50)
        })
    }

照你的写法改的。定时器勿乱用,基本功要扎实。
更重要的是,这样的功能为什么不用CSS?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题