多物体运动 根据传递的属性值改变

来源:互联网 发布:linux c执行shell命令 编辑:程序博客网 时间:2024/05/11 15:56

对物体设置一些操作变透明度  变宽高等  运用到有个unction  getStyle(obj,attr){}函数封装   其中  透明度和其他有区别需要判断  还有浏览器兼容问题   以下代码只适合在IE浏览器中运行


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多物体运动</title>
    <style>
        ul,li{
            list-style: none;
        }
        ul li{
            width:200px;
            height:100px;
            background:yellow;
            margin-bottom: 20px;
            border: 1px solid red;
            filter: alpha(opacity:30);
opacity: 0.3;
        }
        #div1{
        height: 100px;
        width: 200px;
        background: blueviolet;
        margin-left: 40px;
        }
    </style>
    <script>
      window.onload=function(){
      var oLi=document.getElementsByTagName("li");
      var oDiv=document.getElementById("div1");
      for(var i=0;i<oLi.length;i++){
      oLi[i].timer=null;
      oLi[i].onmouseover=function(){
      startMove(this,'opacity',100)
      }
      oLi[i].onmouseout=function(){
      startMove(this,'opacity',30)
      }
      }
      oDiv.timer=null;
      oDiv.onmouseover=function(){
      startMove(this,'height',400)
      }
      oDiv.onmouseout=function(){
      startMove(this,'height',100)
      }
      }
      
      function startMove(obj,attr,iTarget){
      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=(iTarget-icur)/8;
      speed=speed>0?Math.ceil(speed):Math.floor(speed);
      if(icur==iTarget){
      clearInterval(obj.timer);
      }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){//针对IE
      return obj.currentStyle[attr];
     
      }else{//针对火狐(有问题)
      return obj.getComputedStyle(obj,false)[attr];
      }
           
        }
</script>
</head>
<body>
<ul>
   <li ></li>
   <li ></li>
</ul>
<div id="div1"></div>
</body>
</html>


如果想一个运动接着下一个运动————链式运动   

修改如下:

//先变宽再变高  

Li.onmouseover=function(){
      startMove(Li,'width',400,function(){
      startMove(Li,'height',200)
      });
      });

      }
   
      Li.onmouseout=function(){
    startMove(Li,'height',100,function(){
    startMove(Li,'width',200);
    });

    }

 //函数也要改变

function startMove(obj,attr,iTarget,fn){
      clearInterval(obj.timer);
      obj.timer=setInterval(function(){
      //1.获取当前值
      var icur=0;
      if(attr=='opacity'){
      icur=Math.round(parseFloat(getStyle(obj,attr))*100);
     
      }else{
      icur=parseInt(getStyle(obj,attr));
      }
         //2计算速度
      var speed=(iTarget-icur)/8;
      speed=speed>0?Math.ceil(speed):Math.floor(speed);
      //3检测停止
      if(obj.offsetWidth==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)
      }