Skip to content

useRect

获取元素的大小及其相对于视口的位置,等价于 Element.getBoundingClientRect

基本用法

html
<div ref="root" />
js
import { ref, onMounted } from 'vue'
import { useRect } from '@ryxon/use'

export default {
  setup() {
    const root = ref()

    onMounted(() => {
      const rect = useRect(root)
      console.log(rect) // -> 元素的大小及其相对于视口的位置
    })

    return { root }
  }
}

API

类型定义

ts
function useRect(
  element: Element | Window | Ref<Element | Window | undefined>
): DOMRect

返回值

参数说明类型
width宽度number
height高度number
top顶部与视图窗口左上角的距离number
left左侧与视图窗口左上角的距离number
right右侧与视图窗口左上角的距离number
bottom底部与视图窗口左上角的距离number

Released under the MIT License.