Sunday 15 May 2011

Javascript: Check collision between two divs -


There is no way to check, for example if the div name with the name "DIV" overlapping with the name "Ground" Happening?

I want to do this with a clean JavaScript, I know that jQuery is better, but that's what I do not want. I saw this post: but it does not return anything

Thanks for the help.

First, I recommend you to the HTML 5 canvas element, such as With the voice of, you want to make a game, and canvas is great for it;)

But, to answer your question, you can create or Can get div element with document.createElement () or getElementById () respectively, and can get your style properties either your JS-set can get value ( element.style ) Use it if you want to set initial values ​​in CSS.

Be sure that, although you have received these CSS properties, they can be parsed into something that can dig JS. For integer-based situations, parseInt () typically does the trick.

Next, you do the math. In this case, you want to see if the character divisive top and its height is more than the top spot on the ground. If it is, then it has collided.

To set the style back to div, you can simply set the property of style.

Here's an example:

One more thing: When the element elements are complex ways to obtain the position, when the elements use different positioning properties (ex: players top / left coordinates, where the ground uses down ), it is very difficult to manage.

No comments:

Post a Comment