国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript拖拽、碰撞、重力及彈性運動實例分析

2019-11-20 10:50:20
字體:
來源:轉載
供稿:網友

本文實例講述了JavaScript拖拽、碰撞、重力及彈性運動實現方法。分享給大家供大家參考,具體如下:

js拖拽、碰撞與重力實現代碼:

window.onload=function (){ var oDiv=document.getElementById('div1'); var lastX=0; var lastY=0; oDiv.onmousedown=function (ev) { var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; document.onmousemove=function (ev) {  var oEvent=ev||event;  var l=oEvent.clientX-disX;  var t=oEvent.clientY-disY;  oDiv.style.left=l+'px';  oDiv.style.top=t+'px';  iSpeedX=l-lastX;  iSpeedY=t-lastY;  lastX=l;  lastY=t;  document.title='x:'+iSpeedX+', y:'+iSpeedY; }; document.onmouseup=function () {  document.onmousemove=null;  document.onmouseup=null;  startMove(); }; clearInterval(timer); };};var timer=null;var iSpeedX=0;var iSpeedY=0;function startMove(){ clearInterval(timer); timer=setInterval(function (){ var oDiv=document.getElementById('div1'); iSpeedY+=3; var l=oDiv.offsetLeft+iSpeedX; var t=oDiv.offsetTop+iSpeedY; if(t>=document.documentElement.clientHeight-oDiv.offsetHeight) {  iSpeedY*=-0.8;  iSpeedX*=0.8;  t=document.documentElement.clientHeight-oDiv.offsetHeight; } else if(t<=0) {  iSpeedY*=-1;  iSpeedX*=0.8;  t=0; } if(l>=document.documentElement.clientWidth-oDiv.offsetWidth) {  iSpeedX*=-0.8;  l=document.documentElement.clientWidth-oDiv.offsetWidth; } else if(l<=0) {  iSpeedX*=-0.8;  l=0; } if(Math.abs(iSpeedX)<1) {  iSpeedX=0; } if(Math.abs(iSpeedY)<1) {  iSpeedY=0; } if(iSpeedX==0 && iSpeedY==0 && t==document.documentElement.clientHeight-oDiv.offsetHeight) {  clearInterval(timer);  alert('停止'); } else {  oDiv.style.left=l+'px';  oDiv.style.top=t+'px'; } document.title=iSpeedX; }, 30);}

js彈性運動實現代碼:

var left=0; //用left變量存儲賦給obj.style.left的值,以防每次系統都省略小數,所導致最后結果的細微差異var iSpeed=0;function startMove(obj,iTarget){ clearInterval(obj.timer); obj.timer=setInterval(function(){  iSpeed+=(iTarget-obj.offsetLeft)/5; //速度  iSpeed*=0.7; //考慮阻力  left+=iSpeed;  if(Math.abs(iSpeed)<1&&Math.abs(iTarget-obj.offsetLeft)<1) //停止條件 速度和距離絕對值小于1  {   clearInterval(obj.timer);   obj.style.left=iTarget+"px"; //清楚后,順便把目標值賦給obj.style.left  }   else  {   obj.style.left=left+"px";  } },30);}

更多關于JavaScript運動效果相關內容可查看本站專題:《JavaScript運動效果與技巧匯總

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东丽区| 阿坝| 云安县| 台北县| 金堂县| 林口县| 龙游县| 新密市| 兴和县| 旺苍县| 锡林郭勒盟| 邢台市| 嘉义县| 保定市| 梅河口市| 武冈市| 波密县| 华宁县| 苍溪县| 福清市| 平舆县| 睢宁县| 保亭| 申扎县| 东阿县| 巴彦县| 邵阳县| 江津市| 馆陶县| 泉州市| 龙川县| 延长县| 麦盖提县| 鹤岗市| 石台县| 霍邱县| 深圳市| 新巴尔虎右旗| 离岛区| 瑞安市| 铜梁县|