# universal-element

> 获取 DOM 节点的信息.

Latest version **0.0.6** (published 2020-10-22) · 0 weekly downloads

## Install

```sh
npm install universal-element
pnpm add universal-element
yarn add universal-element
bun add universal-element
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2020-10-22 |
| First published | 2020-02-20 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | rax |
| Maintainers | rax-publisher |
| Keywords | Rax |

## Links

- npm: https://www.npmjs.com/package/universal-element
- npm.io page: https://npm.io/package/universal-element

## Dependencies (1)

- [universal-env](https://npm.io/package/universal-env.md) ^3.0.0

## Recent versions

- 0.0.6 (latest) — 2020-10-22
- 2.0.0-beta (beta) — 2021-01-12
- 0.0.5 — 2020-09-16
- 0.0.4 — 2020-08-11
- 0.0.4-0 — 2020-07-31
- 0.0.3 — 2020-03-13
- 0.0.2 — 2020-02-26
- 0.0.1 — 2020-02-20

## README

# universal-element [![npm](https://img.shields.io/npm/v/universal-element.svg)](https://www.npmjs.com/package/universal-element)

获取 DOM 节点的信息.

## 支持
<img alt="miniApp" src="https://gw.alicdn.com/tfs/TB1bBpmbRCw3KVjSZFuXXcAOpXa-200-200.svg" width="25px" height="25px" /> <img alt="wechatMiniprogram" src="https://img.alicdn.com/tfs/TB1slcYdxv1gK0jSZFFXXb0sXXa-200-200.svg" width="25px" height="25px"> <img alt="browser" src="https://gw.alicdn.com/tfs/TB1uYFobGSs3KVjSZPiXXcsiVXa-200-200.svg" width="25px" height="25px" />

## 安装

```bash
$ npm install universal-element --save
```

## 使用

```js
import { createElement, useEffect, Fragment } from 'rax';
import { getScrollOffset, getBoundingClientRect } from 'universal-element';

function App() {
  useEffect(() => {
    getScrollOffset('#container').then((ret) => {
      const { scrollTop, scrollLeft } = ret[0];
      console.log(scrollTop, scrollLeft);
    });

    getBoundingClientRect('#container').then((ret) => {
      const { width, height, top, left, right, bottom } = ret[0];
      console.log(width, height, top, left, right, bottom);
    });
  }, []);

  return (<>
    <View id='container'>test</View>
  </>)
}
```

## 方法

### `getScrollOffset()`

在阿里小程序中, 不存在 `scrollWidth`, `scrollHeight`。

```js
getScrollOffet('#container').then((ret) => {
  const { scrollTop, scrollLeft, scrollWidth, scrollHeight } = ret[0];
  console.log(scrollTop, scrollLeft, scrollWidth, scrollHeight);
});
```

### `getBoundingClientRect()`

在所有小程序中， 不存在 `x`, `y`。

```js
getBoundingClientRect('#container').then((ret) => {
  const { width, height, top, left, right, bottom, x, y } = ret[0];
  console.log(width, height, top, left, right, bottom, x, y);
});
```

### 注意
所有方法在微信小程序的自定义组件中使用的时候，都需要添加第二个参数来指定自定义组件实例：

```js
// 在 Rax 小程序编译时链路参数为 this._internal
getBoundingClientRect('#container', this).then((ret) => {
  const { width, height, top, left, right, bottom, x, y } = ret[0];
  console.log(width, height, top, left, right, bottom, x, y);
});
```

---
_Source: https://npm.io/package/universal-element · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
