Something like this for rect1
and rect2
retrieved via getBoundingClientRect():
var overlap = !(rect1.right < rect2.left ||
rect1.left > rect2.right ||
rect1.bottom < rect2.top ||
rect1.top > rect2.bottom)
Explain: if one or more expressions in the parenthese are true
, there's no overlapping. If all are false
, there must be an overlapping.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…