vant/packages/utils/scroll.js
2019-02-02 10:41:01 +08:00

42 lines
1.3 KiB
JavaScript

import { isServer } from '.';
export const getComputedStyle = !isServer && document.defaultView.getComputedStyle.bind(document.defaultView);
// get nearest scroll element
// http://w3help.org/zh-cn/causes/SD9013
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
export function getScrollEventTarget(element, rootParent = window) {
let node = element;
while (
node &&
node.tagName !== 'HTML' &&
node.tagName !== 'BODY' &&
node.nodeType === 1 &&
node !== rootParent
) {
const { overflowY } = getComputedStyle(node);
if (overflowY === 'scroll' || overflowY === 'auto') {
return node;
}
node = node.parentNode;
}
return rootParent;
}
export function getScrollTop(element) {
return 'scrollTop' in element ? element.scrollTop : element.pageYOffset;
}
export function setScrollTop(element, value) {
'scrollTop' in element ? (element.scrollTop = value) : element.scrollTo(element.scrollX, value);
}
// get distance from element top to page top
export function getElementTop(element) {
return (element === window ? 0 : element.getBoundingClientRect().top) + getScrollTop(window);
}
export function getVisibleHeight(element) {
return element === window ? element.innerHeight : element.getBoundingClientRect().height;
}