chore: prettier all ts code
This commit is contained in:
@@ -15,7 +15,7 @@ if (!isServer) {
|
||||
},
|
||||
});
|
||||
window.addEventListener('test-passive', null as any, opts);
|
||||
// eslint-disable-next-line no-empty
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
@@ -34,11 +34,7 @@ export function on(
|
||||
}
|
||||
}
|
||||
|
||||
export function off(
|
||||
target: EventTarget,
|
||||
event: string,
|
||||
handler: EventHandler
|
||||
) {
|
||||
export function off(target: EventTarget, event: string, handler: EventHandler) {
|
||||
if (!isServer) {
|
||||
target.removeEventListener(event, handler);
|
||||
}
|
||||
|
||||
+42
-21
@@ -4,41 +4,56 @@ type ScrollElement = HTMLElement | Window;
|
||||
// http://w3help.org/zh-cn/causes/SD9013
|
||||
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
|
||||
const overflowScrollReg = /scroll|auto/i;
|
||||
export function getScroller(element: HTMLElement, rootParent: ScrollElement = window) {
|
||||
let node = element;
|
||||
export function getScroller(el: HTMLElement, root: ScrollElement = window) {
|
||||
let node = el;
|
||||
|
||||
while (
|
||||
node &&
|
||||
node.tagName !== 'HTML' &&
|
||||
node.nodeType === 1 &&
|
||||
node !== rootParent
|
||||
node !== root
|
||||
) {
|
||||
const { overflowY } = window.getComputedStyle(node);
|
||||
|
||||
if (overflowScrollReg.test(<string>overflowY)) {
|
||||
if (node.tagName !== 'BODY') {
|
||||
return node;
|
||||
}
|
||||
|
||||
// see: https://github.com/youzan/vant/issues/3823
|
||||
const { overflowY: htmlOverflowY } = window.getComputedStyle(<Element>node.parentNode);
|
||||
const { overflowY: htmlOverflowY } = window.getComputedStyle(
|
||||
<Element>node.parentNode
|
||||
);
|
||||
|
||||
if (overflowScrollReg.test(<string>htmlOverflowY)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
node = <HTMLElement>node.parentNode;
|
||||
}
|
||||
return rootParent;
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
export function getScrollTop(element: ScrollElement): number {
|
||||
return 'scrollTop' in element ? element.scrollTop : element.pageYOffset;
|
||||
export function getScrollTop(el: ScrollElement): number {
|
||||
return 'scrollTop' in el ? el.scrollTop : el.pageYOffset;
|
||||
}
|
||||
|
||||
export function setScrollTop(element: ScrollElement, value: number) {
|
||||
'scrollTop' in element ? (element.scrollTop = value) : element.scrollTo(element.scrollX, value);
|
||||
export function setScrollTop(el: ScrollElement, value: number) {
|
||||
if ('scrollTop' in el) {
|
||||
el.scrollTop = value;
|
||||
} else {
|
||||
el.scrollTo(el.scrollX, value);
|
||||
}
|
||||
}
|
||||
|
||||
export function getRootScrollTop(): number {
|
||||
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
|
||||
return (
|
||||
window.pageYOffset ||
|
||||
document.documentElement.scrollTop ||
|
||||
document.body.scrollTop ||
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
export function setRootScrollTop(value: number) {
|
||||
@@ -47,19 +62,25 @@ export function setRootScrollTop(value: number) {
|
||||
}
|
||||
|
||||
// get distance from element top to page top
|
||||
export function getElementTop(element: ScrollElement) {
|
||||
return (
|
||||
(element === window ? 0 : (<HTMLElement>element).getBoundingClientRect().top) +
|
||||
getRootScrollTop()
|
||||
);
|
||||
export function getElementTop(el: ScrollElement) {
|
||||
if (el === window) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (el as HTMLElement).getBoundingClientRect().top + getRootScrollTop();
|
||||
}
|
||||
|
||||
export function getVisibleHeight(element: ScrollElement) {
|
||||
return element === window
|
||||
? element.innerHeight
|
||||
: (<HTMLElement>element).getBoundingClientRect().height;
|
||||
export function getVisibleHeight(el: ScrollElement) {
|
||||
if (el === window) {
|
||||
return el.innerHeight;
|
||||
}
|
||||
|
||||
return (el as HTMLElement).getBoundingClientRect().height;
|
||||
}
|
||||
|
||||
export function getVisibleTop(element: ScrollElement) {
|
||||
return element === window ? 0 : (<HTMLElement>element).getBoundingClientRect().top;
|
||||
export function getVisibleTop(el: ScrollElement) {
|
||||
if (el === window) {
|
||||
return 0;
|
||||
}
|
||||
return (el as HTMLElement).getBoundingClientRect().top;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
export function isHidden(element: HTMLElement) {
|
||||
const style = window.getComputedStyle(element);
|
||||
const isHidden = style.display === 'none';
|
||||
export function isHidden(el: HTMLElement) {
|
||||
const style = window.getComputedStyle(el);
|
||||
const hidden = style.display === 'none';
|
||||
|
||||
// offsetParent returns null in the following situations:
|
||||
// 1. The element or its parent element has the display property set to none.
|
||||
// 2. The element has the position property set to fixed
|
||||
const isParentHidden = element.offsetParent === null && style.position !== 'fixed';
|
||||
const parentHidden = el.offsetParent === null && style.position !== 'fixed';
|
||||
|
||||
return isHidden || isParentHidden;
|
||||
return hidden || parentHidden;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user