Created
January 31, 2018 02:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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