useRect 元素尺寸
17:43说明
useRect 封装 UniElement.getBoundingClientRectAsync(),异步返回元素相对视口的位置与尺寸;元素为空时直接返回 null。
示例
元素尺寸
模板 ref 拿到 200 × 120 的 view,挂载后调用 getRect 输出 width、height 与四边位置。
查看源码
vue
<template>
<view ref="box" class="demo-rect-box" :style="{ backgroundColor: $u.strOr($u.color.primaryLight) }">
<text class="demo-rect-box-text">200 × 120</text>
</view>
<ux-gap :height="12" />
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in measureRows"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u, useRect } from '@/uni_modules/uview-pro-x';
// 解构方式使用:const { getRect } = useRect()
const { getRect } = useRect();
type Row = {
desc: string;
result: string;
last: boolean;
};
// 模板 ref 拿到的是 UniElement,未渲染时为 null
const box = ref<UniElement | null>(null);
const measureRows = ref<Row[]>([]);
async function measure(): Promise<void> {
const rect = await getRect(box.value);
if (rect == null) {
measureRows.value = [{ desc: '元素尺寸', result: '未取到元素', last: true }];
return;
}
measureRows.value = [
{ desc: 'width', result: rect.width.toFixed(1) + 'px', last: false },
{ desc: 'height', result: rect.height.toFixed(1) + 'px', last: false },
{ desc: 'top', result: rect.top.toFixed(1) + 'px', last: false },
{ desc: 'left', result: rect.left.toFixed(1) + 'px', last: false },
{ desc: 'right', result: rect.right.toFixed(1) + 'px', last: false },
{ desc: 'bottom', result: rect.bottom.toFixed(1) + 'px', last: true }
];
}
// 挂载后再测量,保证元素已完成布局
onMounted((): void => {
measure();
});
</script>
<style lang="scss">
.demo-rect-box {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
width: 200px;
height: 120px;
border-radius: 8px;
border: 1px solid $u-border-color;
}
.demo-rect-box-text {
font-size: 12px;
color: $u-content-color;
}
</style>重新测量
点击按钮再次调用异步的 getRect,元素为 null 时只打印提示,不返回矩形。
查看源码
vue
<template>
<ux-button size="small" type="primary" block text="重新测量" @click="handleMeasure()" />
</template>
<script setup lang="ts">
import { useRect } from '@/uni_modules/uview-pro-x';
const { getRect } = useRect();
const box = ref<UniElement | null>(null);
// getRect 为异步方法,返回 null 表示未取到元素
async function handleMeasure(): Promise<void> {
const rect = await getRect(box.value);
if (rect == null) {
console.log('未取到元素');
return;
}
console.log('width', rect.width, 'height', rect.height);
}
</script>使用说明
以提示条说明 ref 类型为 UniElement | null、底层走 getBoundingClientRectAsync、页面滚动后需重新测量。
查看源码
vue
<template>
<ux-alert-tips type="primary" :show-icon="true" title="const { getRect } = useRect()" />
<ux-gap :height="8" />
<ux-alert-tips type="primary" :show-icon="true" title="ref 类型为 UniElement | null,未渲染时需判空" />
<ux-gap :height="8" />
<ux-alert-tips type="primary" :show-icon="true" title="底层走 getBoundingClientRectAsync,Web / App / 小程序通用" />
<ux-gap :height="8" />
<ux-alert-tips type="primary" :show-icon="true" title="返回相对视口的矩形,页面滚动后需重新测量" />
</template>
<script setup lang="ts">
import { useRect } from '@/uni_modules/uview-pro-x';
const { getRect } = useRect();
</script>API
参数
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| - | 该 Hook 不接受参数 | - | - | - |
返回值
| 字段 | 说明 | 类型 |
|---|---|---|
| getRect | 异步获取元素的位置与尺寸;元素为 null 时返回 null | (el: UniElement | null) => Promise<DOMRect | null> |
注意事项
- 入参是模板 ref 得到的
UniElement,未渲染时为null,此时直接返回null。 - 返回值为相对视口的矩形(
width/height/top/left/right/bottom),页面滚动或布局变化后需重新测量。 - 底层调用
getBoundingClientRectAsync,无需createSelectorQuery;源码同时导出函数式getRect(el),供非 setup 场景直接调用。