domUtils.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { upperFirst } from 'lodash-es';
  2. export interface ViewportOffsetResult {
  3. left: number;
  4. top: number;
  5. right: number;
  6. bottom: number;
  7. rightIncludeBody: number;
  8. bottomIncludeBody: number;
  9. }
  10. export function getBoundingClientRect(element: Element): DOMRect | number {
  11. if (!element || !element.getBoundingClientRect) {
  12. return 0;
  13. }
  14. return element.getBoundingClientRect();
  15. }
  16. function trim(string: string) {
  17. return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
  18. }
  19. /* istanbul ignore next */
  20. export function hasClass(el: Element, cls: string) {
  21. if (!el || !cls) return false;
  22. if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.');
  23. if (el.classList) {
  24. return el.classList.contains(cls);
  25. } else {
  26. return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
  27. }
  28. }
  29. /* istanbul ignore next */
  30. export function addClass(el: Element, cls: string) {
  31. if (!el) return;
  32. let curClass = el.className;
  33. const classes = (cls || '').split(' ');
  34. for (let i = 0, j = classes.length; i < j; i++) {
  35. const clsName = classes[i];
  36. if (!clsName) continue;
  37. if (el.classList) {
  38. el.classList.add(clsName);
  39. } else if (!hasClass(el, clsName)) {
  40. curClass += ' ' + clsName;
  41. }
  42. }
  43. if (!el.classList) {
  44. el.className = curClass;
  45. }
  46. }
  47. /* istanbul ignore next */
  48. export function removeClass(el: Element, cls: string) {
  49. if (!el || !cls) return;
  50. const classes = cls.split(' ');
  51. let curClass = ' ' + el.className + ' ';
  52. for (let i = 0, j = classes.length; i < j; i++) {
  53. const clsName = classes[i];
  54. if (!clsName) continue;
  55. if (el.classList) {
  56. el.classList.remove(clsName);
  57. } else if (hasClass(el, clsName)) {
  58. curClass = curClass.replace(' ' + clsName + ' ', ' ');
  59. }
  60. }
  61. if (!el.classList) {
  62. el.className = trim(curClass);
  63. }
  64. }
  65. /**
  66. * Get the left and top offset of the current element
  67. * left: the distance between the leftmost element and the left side of the document
  68. * top: the distance from the top of the element to the top of the document
  69. * right: the distance from the far right of the element to the right of the document
  70. * bottom: the distance from the bottom of the element to the bottom of the document
  71. * rightIncludeBody: the distance between the leftmost element and the right side of the document
  72. * bottomIncludeBody: the distance from the bottom of the element to the bottom of the document
  73. *
  74. * @description:
  75. */
  76. export function getViewportOffset(element: Element): ViewportOffsetResult {
  77. const doc = document.documentElement;
  78. const docScrollLeft = doc.scrollLeft;
  79. const docScrollTop = doc.scrollTop;
  80. const docClientLeft = doc.clientLeft;
  81. const docClientTop = doc.clientTop;
  82. const pageXOffset = window.pageXOffset;
  83. const pageYOffset = window.pageYOffset;
  84. const box = getBoundingClientRect(element);
  85. const { left: retLeft, top: rectTop, width: rectWidth, height: rectHeight } = box as DOMRect;
  86. const scrollLeft = (pageXOffset || docScrollLeft) - (docClientLeft || 0);
  87. const scrollTop = (pageYOffset || docScrollTop) - (docClientTop || 0);
  88. const offsetLeft = retLeft + pageXOffset;
  89. const offsetTop = rectTop + pageYOffset;
  90. const left = offsetLeft - scrollLeft;
  91. const top = offsetTop - scrollTop;
  92. const clientWidth = window.document.documentElement.clientWidth;
  93. const clientHeight = window.document.documentElement.clientHeight;
  94. return {
  95. left: left,
  96. top: top,
  97. right: clientWidth - rectWidth - left,
  98. bottom: clientHeight - rectHeight - top,
  99. rightIncludeBody: clientWidth - left,
  100. bottomIncludeBody: clientHeight - top,
  101. };
  102. }
  103. export function hackCss(attr: string, value: string) {
  104. const prefix: string[] = ['webkit', 'Moz', 'ms', 'OT'];
  105. const styleObj: any = {};
  106. prefix.forEach((item) => {
  107. styleObj[`${item}${upperFirst(attr)}`] = value;
  108. });
  109. return {
  110. ...styleObj,
  111. [attr]: value,
  112. };
  113. }
  114. /* istanbul ignore next */
  115. export function on(
  116. element: Element | HTMLElement | Document | Window,
  117. event: string,
  118. handler: EventListenerOrEventListenerObject
  119. ): void {
  120. if (element && event && handler) {
  121. element.addEventListener(event, handler, false);
  122. }
  123. }
  124. /* istanbul ignore next */
  125. export function off(
  126. element: Element | HTMLElement | Document | Window,
  127. event: string,
  128. handler: Fn
  129. ): void {
  130. if (element && event && handler) {
  131. element.removeEventListener(event, handler, false);
  132. }
  133. }
  134. /* istanbul ignore next */
  135. export function once(el: HTMLElement, event: string, fn: EventListener): void {
  136. const listener = function (this: any, ...args: unknown[]) {
  137. if (fn) {
  138. fn.apply(this, args);
  139. }
  140. off(el, event, listener);
  141. };
  142. on(el, event, listener);
  143. }