How to detect overlap HTML elements, using JavaScript?
I have an item list (<ul>
). It slides up and down using JavaScript. When it slides down, depending on number of item, it may overlap the other element (<div>
), which is absolute positioned at this time (it can be changed).
How I can detect, when <ul>
overlaps this <div>
, to apply new style to the <div>
to hide it temporary or to move it down a little bit, to prevent overlapping?
It's just not looking good, when they overlap))
Here you can see, what I'm talking about: http://timetable.raj.fvds.ru/
Thanks a lot!
const arr = [ { start: '01:00', end: '04:00' }, { start: '05:00', end: '08:00' }, { start: '07:00', end: '11:00' }, { start: '09:30', end: '18:00' }, ]; Our function should iterate through this array of objects and check all elements of the array against the others.
Positioning is used with elements that overlap with each other.
Just remove the min-width from your CSS! And give min-width to the container with margin: auto to make it center. Show activity on this post. Take out the min-width CSS.
Elements can overlap for a variety of reasons, for instance, relative positioning has nudged it over something else. Negative margin has pulled the element over another. Absolutely positioned elements overlap each other.
function isObjOnObj(a,b){
var al = a.left;
var ar = a.left+a.width;
var bl = b.left;
var br = b.left+b.width;
var at = a.top;
var ab = a.top+a.height;
var bt = b.top;
var bb = b.top+b.height;
if(bl>ar || br<al){return false;}//overlap not possible
if(bt>ab || bb<at){return false;}//overlap not possible
if(bl>al && bl<ar){return true;}
if(br>al && br<ar){return true;}
if(bt>at && bt<ab){return true;}
if(bb>at && bb<ab){return true;}
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With