两个div方块,碰撞到一起后其中一个方块变色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body{margin: 0;} #div1{width: 300px; height: 300px; background: red; position: absolute; left: 0; top: 0; } #div2{width: 200px; height: 200px; background: rgb(25, 245, 110); position: absolute; left: 600px; top: 360px; } </style> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html> <script> var div1=document.getElementById('div1') var div2=document.getElementById('div2') div1.onmousedown=function(event){ var chaX=event.clientX-div1.offsetLeft var chaY=event.clientY-div1.offsetTop console.log(chaX+"sasa") document.onmousemove=function(event){ div1.style.left=event.clientX-chaX+'px' div1.style.top=event.clientY-chaY+'px' if(div1.offsetLeft+div1.offsetWidth>=div2.offsetLeft&&div1.offsetTop+div1.offsetWidth>=div2.offsetTop &&div1.offsetLeft<=div2.offsetLeft+div2.offsetWidth&&div1.offsetTop<=div2.offsetTop+div2.offsetWidth ){ div2.style.background='#000000' }else{div2.style.background=' rgb(25, 245, 110)'} } document.onmouseup=function(){ document.onmousemove=null } } // div1.style.left>div2.style.left-300+'px'&&div1.style.top>div2.style.top </script>