domUtils.ts 5.4 KB

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