Vant4 useRect

介绍

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

代码演示

基本用法

<div ref="root" />
import { ref, onMounted } from vue;
import { useRect } from @vant/use;

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

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

    return { root };
  },
};

API

类型定义

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

返回值

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

作者:andy,如若转载,请注明出处:https://www.web176.com/vant4/18862.html

(0)
打赏 支付宝 支付宝 微信 微信
andy的头像andy
上一篇 2023年4月28日
下一篇 2023年4月28日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注