js碰撞事件
阿新 • • 發佈:2021-12-08
兩個盒子碰撞發生顏色改變:
<html>:
<divid="div1"></div> <divid="div2"></div><css>:
body{ margin:0; } #div1{ width:200px; height:200px; background-color:rgb(51,255,0); position:absolute; left:0; top:0; } #div2{ width:200px; height:200px; background-color:rgb(71,0,163); position:absolute; left:500px; top:200px; } <js>:functionmove(obj){ obj.onmousedown=function(){ varchaX=event.clientX-obj.offsetLeft; varchaY=event.clientY-obj.offsetTop;
document.onmousemove=function(event){ obj.style.left=event.clientX-chaX+"px"; obj.style.top=event.clientY-chaY+"px"; crash(div1,div2) };
document.onmouseup=function(){ document.onmousemove=null; }; }; } //封裝完成後輸出 move(div1) move(div2)