Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **style样式**
- ```
- <style>
- .box{
- width:100px;
- height:100px;
- background-color:#ff9c69;
- font-size:20px;
- position:absolute;
- left:50px;
- text-align:center;
- line-height:100px;
- user-select:none;
- }
- </style>
- ```
- **文本内容**
- ```
- <div class="box"></div>
- <div class="box" style="background-color:red;margin-left:400px;"></div>
- ```
- **js样式**
- ```
- <script>
- var box=document.getElementsByClassName("box")[0];
- var box2=document.getElementsByClassName("box")[1];
- box.onmousedown=function(e){
- var ev=e||event;
- var divX=ev.offsetX;
- var divY=ev.offsetY;
- document.onmosuemvoe=function(){
- var ev=e||event;
- var x=ev.clientX-divX;
- var y=ev.clientY-divY;
- box.style.left=x+"px";
- box.style.top=y+"px";
- var moveL=box.offsetLeft;
- var moveR=box.offsetLeft+box.offsetWidth;
- var moveT=box.offsetTop;
- var moveB=box.offsetTop+box.offsetHeight;
- var hitL=box2.offsetLeft;
- var hitR=box2.offsetLeft+box2.offsetWidth;
- var hitT=box2.offsetTop;
- var hitB=box2.offsetTop+box2.offsetHeight;
- var r1=moveR>=hitL;
- var r2=moveL<=hitR;
- var r3=moveT<=hitB;
- var r4=moveB>=hitT;
- if(r1&&r2&&r3&&r4){
- box2.style.backgroundColor="red";
- box2.innerHTML="被撞了";
- }else{
- box2.style.backgroundColor="red";
- box2.innerHTML="";
- }
- }
- }
- </script>
- ```
- 情况就是这样了。
Add Comment
Please, Sign In to add comment