js多個物體運動功能執行個體分析,js多個物體執行個體分析

來源:互聯網
上載者:User

js多個物體運動功能執行個體分析,js多個物體執行個體分析

本文執行個體分析了js實現的多個物體運動功能。分享給大家供大家參考,具體如下:

與單個的區別:得知道哪個在動,所以運動函數需要兩個參數,出了目標iTarget之外,還要obj。另外需要多個計數器,否則當一個還沒運動完就移入另一個物體會發生卡殼

window.onload=function(){  var aDiv=document.getElementsByTagName("div");  var timer=null;  var i;  for(i=0;i<aDiv.length;i++){    aDiv[i].timer=null;    aDiv[i].onmouseover=function(){      startMove(this,300);    };    aDiv[i].onmouseout=function(){      startMove(this,100);    };  }  function startMove(obj,iTarget){    clearInterval(obj.timer);    obj.timer=setInterval(function(){      var iSpeed=(iTarget-obj.offsetWidth)/8;      iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);  //凡是要用緩衝運動,肯定缺不了取整,為了讓他能到目的點(不然無法完全貼合)      if(obj.offsetWidth==iTarget){        clearInterval(obj.timer);      }      else{        obj.style.width=obj.offsetWidth+iSpeed+'px';      }    },30);  };};

註:多物體運動,所有東西都不能公用

屬性與運動對象綁定:速度、其他屬性值(如透明度等)

offsetWidth、offsetHeight、offsetLeft、offsetHeight都有一個bug,拿offsetWidth舉例,他除了width還包括padding和border,比如div寬度為100,還有一像素邊框,現在讓div運動,div.style.width=div.offsetWidth-1+'px',沒有邊框的的情況下他會一直縮小直到消失,有邊框的情況。width:100px,offsetWidth:102px >>>>>width:101px,offsetWidth:103px,會使得他不斷變大

解決辦法:

用 currentStyle  div.style.width=parseInt(getStyle(div,'width'))-1+'px'   getStyle是自己封裝好的擷取樣式的函數,裡麵包括currentStyle方法。parseInt解析字串返回整數。

擴充(任意值變化):

用同一套運動架構使得一個物體變寬,一個變高,一一個透明度變化

window.onload=function(){  var aDiv=document.getElementsByTagName("div");  var timer=null;  aDiv[0].onmouseover=function(){    startMove(this,'width',300);  };  aDiv[0].onmouseout=function(){    startMove(this,'width',100);  };  function getStyle(obj,attr){    if(obj.currentStyle){      return obj.currentStyle[attr];    }    else{      return getComputedStyle(obj,false)[attr];    }  };  function startMove(obj,attr,iTarget){    clearInterval(obj.timer);    obj.timer=setInterval(function(){      var iCur=parseInt(getStyle(obj,attr));      var iSpeed=(iTarget-iCur)/8;      iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);  //凡是要用緩衝運動,肯定缺不了取整,為了讓他能到目的點(不然無法完全貼合)      if(iCur==iTarget){        clearInterval(obj.timer);      }      else{        obj.style[attr]=iCur+iSpeed+'px';      }    },30);  };};

這套運動架構還有個問題,透明度沒支援

1.

var iCur=parseInt(getStyle(obj,attr));

opacity取到的都是零點幾,parseInt取整,所以opacity永遠是0,應改為

if(attr=='opacity'){  var iCur=parseFloat(getStyle(obj,attr))*100;  //為了其他程式不用修改,這裡統一乘100}else{  var iCur=parseInt(getStyle(obj,attr));}

2.

obj.style[attr]=iCur+iSpeed+'px';

按現在寫法就是

aDiv.style.opacity=50px;

應改為

if(attr=='opacity'){  obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';  obj.style.opacity=(iCur+iSpeed)/100;}else{  obj.style[attr]=iCur+iSpeed+'px'}

3.電腦內部,都是類比的來儲存小數,不是實際來儲存,最簡單的例子

alert(0.07*100);  //輸出並不是7,而是7.0000...001,不止7,很多數字(小數)都有問題

所以var iCur=parseFloat(getStyle(obj,attr))*100;就會出問題(會閃爍),解決方案就是避免使用小數

var iCur=parseInt(parseFloat(getStyle(obj,attr))*100);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.