isHidden.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var root = require('./root');
  2. var getComputedStyle = root.getComputedStyle;
  3. var document = root.document;
  4. exports = function(el) {
  5. var _ref =
  6. arguments.length > 1 && arguments[1] !== undefined
  7. ? arguments[1]
  8. : {},
  9. _ref$display = _ref.display,
  10. display = _ref$display === void 0 ? true : _ref$display,
  11. _ref$visibility = _ref.visibility,
  12. visibility = _ref$visibility === void 0 ? false : _ref$visibility,
  13. _ref$opacity = _ref.opacity,
  14. opacity = _ref$opacity === void 0 ? false : _ref$opacity,
  15. _ref$size = _ref.size,
  16. size = _ref$size === void 0 ? false : _ref$size,
  17. _ref$viewport = _ref.viewport,
  18. viewport = _ref$viewport === void 0 ? false : _ref$viewport,
  19. _ref$overflow = _ref.overflow,
  20. overflow = _ref$overflow === void 0 ? false : _ref$overflow;
  21. var computedStyle = getComputedStyle(el);
  22. if (display) {
  23. var tagName = el.tagName;
  24. if (
  25. tagName === 'BODY' ||
  26. tagName === 'HTML' ||
  27. computedStyle.position === 'fixed'
  28. ) {
  29. if (computedStyle.display === 'none') {
  30. return true;
  31. } else {
  32. var cur = el;
  33. while ((cur = cur.parentElement)) {
  34. var _computedStyle = getComputedStyle(cur);
  35. if (_computedStyle.display === 'none') {
  36. return true;
  37. }
  38. }
  39. }
  40. } else if (el.offsetParent === null) {
  41. return true;
  42. }
  43. }
  44. if (visibility && computedStyle.visibility === 'hidden') {
  45. return true;
  46. }
  47. if (opacity) {
  48. if (computedStyle.opacity === '0') {
  49. return true;
  50. } else {
  51. var _cur = el;
  52. while ((_cur = _cur.parentElement)) {
  53. var _computedStyle2 = getComputedStyle(_cur);
  54. if (_computedStyle2.opacity === '0') {
  55. return true;
  56. }
  57. }
  58. }
  59. }
  60. var clientRect = el.getBoundingClientRect();
  61. if (size && (clientRect.width === 0 || clientRect.height === 0)) {
  62. return true;
  63. }
  64. if (viewport) {
  65. var containerRect = {
  66. top: 0,
  67. left: 0,
  68. right: document.documentElement.clientWidth,
  69. bottom: document.documentElement.clientHeight
  70. };
  71. return isOutside(clientRect, containerRect);
  72. }
  73. if (overflow) {
  74. var _cur2 = el;
  75. while ((_cur2 = _cur2.parentElement)) {
  76. var _computedStyle3 = getComputedStyle(_cur2);
  77. var _overflow = _computedStyle3.overflow;
  78. if (_overflow === 'scroll' || _overflow === 'hidden') {
  79. var curRect = _cur2.getBoundingClientRect();
  80. if (isOutside(clientRect, curRect)) return true;
  81. }
  82. }
  83. }
  84. return false;
  85. };
  86. function isOutside(clientRect, containerRect) {
  87. return (
  88. clientRect.right < containerRect.left ||
  89. clientRect.left > containerRect.right ||
  90. clientRect.bottom < containerRect.top ||
  91. clientRect.top > containerRect.bottom
  92. );
  93. }
  94. module.exports = exports;