diff --git a/src/hooks/useHeights.tsx b/src/hooks/useHeights.tsx index 9a6bb832..d1c6a5fa 100644 --- a/src/hooks/useHeights.tsx +++ b/src/hooks/useHeights.tsx @@ -4,6 +4,7 @@ import findDOMNode from 'rc-util/lib/Dom/findDOMNode'; import raf from 'rc-util/lib/raf'; import type { GetKey } from '../interface'; import CacheMap from '../utils/CacheMap'; +import { getOuterHeight } from '../utils/outerHeight'; export default function useHeights( getKey: GetKey, @@ -26,9 +27,10 @@ export default function useHeights( instanceRef.current.forEach((element, key) => { if (element && element.offsetParent) { const htmlElement = findDOMNode(element); - const { offsetHeight } = htmlElement; - if (heightsRef.current.get(key) !== offsetHeight) { - heightsRef.current.set(key, htmlElement.offsetHeight); + const outerHeight = getOuterHeight(htmlElement) + + if (heightsRef.current.get(key) !== outerHeight) { + heightsRef.current.set(key, outerHeight); } } }); diff --git a/src/utils/outerHeight.ts b/src/utils/outerHeight.ts new file mode 100644 index 00000000..cb80ed3d --- /dev/null +++ b/src/utils/outerHeight.ts @@ -0,0 +1,10 @@ +/** + * To get exact height to avoid scrolling deviation + */ +export const getOuterHeight = (el: HTMLElement) => { + let height = el.offsetHeight; + const computedStyle = window.getComputedStyle(el); + height += parseInt(computedStyle.marginTop, 10); + height += parseInt(computedStyle.marginBottom, 10); + return height; +}