user image

Eslam Zedan
Published in : 2022-03-02

Check if div have touch another div

Javascript

I Want To check if my div have touch another div

here is my Work

$(document).mousemove(function(event){ if(getmouse){ var newX= event.pageX-20; var newY= event.pageY-20; $('.mybox').css('left',newX + "px"); $('.mybox').css('top',newY+ "px"); var checkY = parseInt($('.box1').css('top').slice(0,-2)); var checkX = parseInt($('.box1').css('left').slice(0,-2)); var padingval = parseInt($('.box').css('padding').slice(0,-2))*2; console.log(checkY+padingval+" - "+newY); if((newY==checkY&&newX==checkX)||newY+20<checkY-padingval ||newY-20<checkY+padingval) { alert('Game Over'); getmouse = false; $('.mybox').css('top',"240px"); $('.mybox').css('left',"420px"); } } })

//jsfiddle.net/48m6rof6

if My div Touch any div it Must alert Game over and return to its place I have checked if it touched first box but if there is any simple way to check ?

Comments

Rakshit Date : 2022-03-03

Best answers

34

Best answers

34

Your need is to find the overlap between HTML elements.

To achieve your requirements, you need to use some readymade API like jQuery Collision API.

var isCollisionOccurred = $("#box1").collision(".box2");

Visit documentation for more details: Collision API Doc & Usage for collision library.

You can check which div is inside and outside of the block when you drag and drop it through the canvas or your window boundary. Visit above links to see more about the API capabilities.

Reference: [Thread1]

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

How to get audio input and audio output as one stream?
Publish date: 2022-02-13 | Comments: 2

Tag: Javascript

What is the use of package-lock JSON file?
Publish date: 2022-02-11 | Comments: 3

Tag: Javascript

Help: How do I loop simple JSON objects to iterate all keys and values itself?
Publish date: 2022-03-06 | Comments: 1

Tag: Javascript

Javascript: Filtering arrays by it properties?
Publish date: 2022-03-05 | Comments: 1

Tag: Javascript

Can I read a local json, image or text file from Javascript?
Publish date: 2022-03-03 | Comments: 1

Tag: Javascript

JAVASCRIPT: How do I add another key to object?
Publish date: 2022-03-06 | Comments: 3

Tag: Javascript

Adding an id number at the first index to an array of objects
Publish date: 2022-02-13 | Comments: 1

Tag: Javascript

Method refactoring to avoid to many npe checks
Publish date: 2022-03-05 | Comments: 0

Tag: Javascript