diff --git a/src/dropdown-menu/README.zh-CN.md b/src/dropdown-menu/README.zh-CN.md index c346cdd57..c1dcbc259 100644 --- a/src/dropdown-menu/README.zh-CN.md +++ b/src/dropdown-menu/README.zh-CN.md @@ -165,10 +165,10 @@ export default { ### DropdownItem Slots -| 名称 | 说明 | -| ------- | -------------------------- | -| default | 菜单内容 | -| title | 自定义标题,不支持动态渲染 | +| 名称 | 说明 | +| ------- | ---------------- | +| default | 菜单内容 | +| title | 自定义菜单项标题 | ### DropdownItem 方法 diff --git a/src/index-anchor/index.js b/src/index-anchor/index.js index df74a9200..f55295412 100644 --- a/src/index-anchor/index.js +++ b/src/index-anchor/index.js @@ -1,12 +1,13 @@ -import { reactive, ref, computed } from 'vue'; +import { ref, reactive, computed, onMounted } from 'vue'; // Utils import { createNamespace } from '../utils'; import { BORDER_BOTTOM } from '../utils/constant'; import { INDEX_BAR_KEY } from '../index-bar'; +import { getScrollTop, getRootScrollTop } from '../utils/dom/scroll'; // Composition -import { useParent } from '@vant/use'; +import { useRect, useParent } from '@vant/use'; import { useHeight } from '../composition/use-height'; import { useExpose } from '../composition/use-expose'; @@ -21,12 +22,12 @@ export default createComponent({ const state = reactive({ top: 0, left: null, + rect: { top: 0, height: 0 }, width: null, active: false, }); const root = ref(); - const height = useHeight(root); const { parent } = useParent(INDEX_BAR_KEY); const isSticky = () => state.active && parent.props.sticky; @@ -45,16 +46,37 @@ export default createComponent({ } }); + const getRect = (scrollParent, scrollParentRect) => { + const rootRect = useRect(root); + state.rect.height = rootRect.height; + + if (scrollParent === window || scrollParent === document.body) { + state.rect.top = rootRect.top + getRootScrollTop(); + } else { + state.rect.top = + rootRect.top + getScrollTop(scrollParent) - scrollParentRect.top; + } + + return state.rect; + }; + + onMounted(() => { + state.rect.height = useHeight(root); + }); + useExpose({ state, - height, + getRect, }); return () => { const sticky = isSticky(); return ( -