mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(@vant/use): add useRect
This commit is contained in:
parent
5f2de02cc2
commit
8d4135a690
@ -26,6 +26,7 @@ module.exports = {
|
|||||||
title: 'DOM',
|
title: 'DOM',
|
||||||
collapsable: false,
|
collapsable: false,
|
||||||
children: [
|
children: [
|
||||||
|
'/src/useRect/',
|
||||||
'/src/useClickAway/',
|
'/src/useClickAway/',
|
||||||
'/src/useEventListener/',
|
'/src/useEventListener/',
|
||||||
'/src/usePageVisibility/',
|
'/src/usePageVisibility/',
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
export { useRect } from './useRect';
|
||||||
export { useToggle } from './useToggle';
|
export { useToggle } from './useToggle';
|
||||||
export { useClickAway } from './useClickAway';
|
export { useClickAway } from './useClickAway';
|
||||||
export { useWindowSize } from './useWindowSize';
|
export { useWindowSize } from './useWindowSize';
|
||||||
|
46
packages/vant-use/src/useRect/README.md
Normal file
46
packages/vant-use/src/useRect/README.md
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# useRect
|
||||||
|
|
||||||
|
获取元素的大小及其相对于视口的位置,等价于 [Element.getBoundingClientRect](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect)。
|
||||||
|
|
||||||
|
## 代码演示
|
||||||
|
|
||||||
|
### 基本用法
|
||||||
|
|
||||||
|
```html
|
||||||
|
<div ref="root" />
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { root } from 'vue';
|
||||||
|
import { useRect } from '@vant/use';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
setup() {
|
||||||
|
const root = ref();
|
||||||
|
const rect = useRect();
|
||||||
|
|
||||||
|
console.log(rect); // -> 元素的大小及其相对于视口的位置
|
||||||
|
|
||||||
|
return { root };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### 类型定义
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function useRect((Element | Window) | Ref<Element | Window>): DOMRect;
|
||||||
|
```
|
||||||
|
|
||||||
|
### 返回值
|
||||||
|
|
||||||
|
| 参数 | 说明 | 类型 |
|
||||||
|
| ------ | -------------------------- | -------- |
|
||||||
|
| width | 宽度 | _number_ |
|
||||||
|
| height | 高度 | _number_ |
|
||||||
|
| top | 顶部与视图窗口左上角的距离 | _number_ |
|
||||||
|
| left | 左侧与视图窗口左上角的距离 | _number_ |
|
||||||
|
| right | 右侧与视图窗口左上角的距离 | _number_ |
|
||||||
|
| bottom | 底部与视图窗口左上角的距离 | _number_ |
|
38
packages/vant-use/src/useRect/index.ts
Normal file
38
packages/vant-use/src/useRect/index.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { Ref, unref } from 'vue';
|
||||||
|
|
||||||
|
function isWindow(val: unknown): val is Window {
|
||||||
|
return val === window;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useRect = (
|
||||||
|
elementRef: (Element | Window) | Ref<Element | Window>
|
||||||
|
) => {
|
||||||
|
const element = unref(elementRef);
|
||||||
|
|
||||||
|
if (isWindow(element)) {
|
||||||
|
const width = element.innerWidth;
|
||||||
|
const height = element.innerHeight;
|
||||||
|
|
||||||
|
return {
|
||||||
|
top: 0,
|
||||||
|
left: 0,
|
||||||
|
right: width,
|
||||||
|
bottom: height,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.getBoundingClientRect) {
|
||||||
|
return element.getBoundingClientRect();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
top: 0,
|
||||||
|
left: 0,
|
||||||
|
right: 0,
|
||||||
|
bottom: 0,
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
};
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user