$.fn.isClickable = function() {
if (!this.length) return false;
const getZIndex = e => {
if (e === window || e === document) return 0;
var z = document.defaultView.getComputedStyle(e).getPropertyValue('z-index');
if (isNaN(z)) return getZIndex(e.parentNode);
else return z;
};
var width = this.width(),
height = this.height(),
offset = this.offset(),
zIndex = getZIndex(this[0]),
clickable = true,
target = this[0],
targetIsBefore = false;
$("body *").each(function() {
if (this === target) targetIsBefore = true;
if (!$(this).is(":visible") || this === target) return;
var e_width = $(this).width(),
e_height = $(this).height(),
e_offset = $(this).offset(),
e_zIndex = getZIndex(this),
leftOfTarget = offset.left >= e_offset.left,
rightOfTarget = width + offset.left <= e_width + e_offset.left,
belowTarget = offset.top >= e_offset.top,
aboveTarget = height + offset.top <= e_height + e_offset.top,
behindTarget = e_zIndex === zIndex ? targetIsBefore : e_zIndex > zIndex;
if (leftOfTarget && rightOfTarget && belowTarget && aboveTarget && behindTarget) clickable = false;
});
return clickable;
};
$(".clickme").click(function() {
alert("u clicked " + this.id)
});
$(".clickme").each(function() {
console.log("#"+this.id, $(this).isClickable() ? "is clickable" : "is NOT clickable");
})
#item1 {
background: rgba(230, 30, 43, 0.3);
position: absolute;
top: 3px;
left: 4px;
width: 205px;
height: 250px;
}
#item2 {
background: rgba(30, 250, 43, 0.3);
position: absolute;
top: 100px;
left: 50px;
width: 148px;
height: 50px;
}
#item3 {
background: rgba(30, 25, 110, 0.3);
position: absolute;
top: 23px;
left: 101px;
width: 32px;
height: 100px;
}
#item4 {
background: rgba(159, 25, 110, 0.3);
position: absolute;
top: 10px;
left: 45px;
width: 23px;
height: 45px;
z-index: -111
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="item1" class='clickme'></div>
<div id="item2" class='clickme'></div>
<div id="item3" class='clickme'></div>
<div id="item4" class='clickme'></div>