Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Created January 31, 2018 02:54
Show Gist options
  • Select an option

  • Save Pamblam/fe15a99d45892fee1cd2ab14109d876e to your computer and use it in GitHub Desktop.

Select an option

Save Pamblam/fe15a99d45892fee1cd2ab14109d876e to your computer and use it in GitHub Desktop.
Determine if the first element in a jQuery set is accessible to the user
/**
* Determine if the first element in a jQuery set is accessible to the user
*/
$.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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment