mirror of
https://github.com/youzan/vant.git
synced 2026-04-06 02:00:43 +08:00
[bugfix] compatible <body /> is the scene of the scrolling container (#3844)
This commit is contained in:
@@ -3,18 +3,26 @@ type ScrollElement = HTMLElement | Window;
|
||||
// get nearest scroll element
|
||||
// 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 getScrollEventTarget(element: HTMLElement, rootParent: ScrollElement = window) {
|
||||
let node = element;
|
||||
while (
|
||||
node &&
|
||||
node.tagName !== 'HTML' &&
|
||||
node.tagName !== 'BODY' &&
|
||||
node.nodeType === 1 &&
|
||||
node !== rootParent
|
||||
) {
|
||||
const { overflowY } = window.getComputedStyle(node);
|
||||
if (overflowY === 'scroll' || overflowY === 'auto') {
|
||||
return 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);
|
||||
if (overflowScrollReg.test(<string>htmlOverflowY)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
node = <HTMLElement>node.parentNode;
|
||||
}
|
||||
@@ -33,11 +41,16 @@ export function getRootScrollTop(): number {
|
||||
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
|
||||
}
|
||||
|
||||
export function setRootScrollTop(value: number) {
|
||||
setScrollTop(window, value);
|
||||
setScrollTop(document.body, value);
|
||||
}
|
||||
|
||||
// get distance from element top to page top
|
||||
export function getElementTop(element: ScrollElement) {
|
||||
return (
|
||||
(element === window ? 0 : (<HTMLElement>element).getBoundingClientRect().top) +
|
||||
getScrollTop(window)
|
||||
getRootScrollTop()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user