//@/components/base/create-hashtag/TagInput.vue
<template>
...
<HashtagMenu
v-for="(item, index) in filterTags"
:ref="el=> {menuItemRef[index]= el}"
:key="index"
:index="index"
:item="item"
:active-index="choiceTagsIndex"
@click="clickOnMenu(item)"
/>
...
</template>
<script>
export default defineComponent({
setup(props, { emit }) {
const menuItemRef = ref<typeof HashtagMenu[]>([])
const handleOnChoiceTag = () =>{
//변경 전
const scrollPosition = menuItemRef.value[choiceTagsIndex.value].getBoundingClientRect()
//변경 후
const scrollPosition = menuItemRef.value[choiceTagsIndex.value].$el.getBoundingClientRect()
}
}
})
</script>
방법) scrollIntoView() 사용
[element.scrollIntoView - Web API | MDN](https://developer.mozilla.org/ko/docs/Web/API/Element/scrollIntoView)
export default defineComponent({
setup(props, { emit }) {
const menuItemRef = ref<typeof HashtagMenu[]>([])
const handleOnChoiceTag = () =>{
menuItemRef.value[choiceTagsIndex.value].scrollIntoView()
}
}
})
아래 함수를 이용해서 해당 아이템이 현재 화면에 있는지 체크 할 수 있다.
export const detectElementInViewport = (el: Element, scrollContainer: HTMLElement | null) => {
const rect = el.getBoundingClientRect()
const scrollRect = scrollContainer?.getBoundingClientRect()
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= scrollContainer!.clientHeight + scrollRect!.top &&
rect.right <= scrollContainer!.clientWidth + scrollRect!.right
)
}
https://github.com/vue-final/vue-final-modal/issues/10