---
name: "IntersectionObserver.observe"
mode: "api"
versionRequirements:
  - { name: "Base Library", version: "2.10.0" }
title: "observe"
---

## IntersectionObserver.observe

> [VERSION] Base Library >= 2.10.0

### Description

Specify the target node and start observing intersection changes

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `selector` | `string` | Yes | Target node selector |
| `callback` | `(entry: Object) => void` | Yes | Callback function invoked when the intersection state changes |

##### IntersectionObserver.observe.callback(entry) properties

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `time` | `number` | Yes | - | Timestamp when the intersection is evaluated |
| `relativeRect` | `Margins` | Yes | - | Bounds of the reference region |
| `boundingClientRect` | `DOMRect` | Yes | - | Bounds of the target node |
| `intersectionRect` | `DOMRect` | Yes | - | Bounds of the intersection area |
| `intersectionRatio` | `number` | Yes | - | Intersection ratio |
| `dataset` | `Record<string, any>` | Yes | - | The target node’s dataset |
| `id` | `string` | Yes | - | The target node’s ID |


### Return Value

Type: `IntersectionObserver`

[IntersectionObserver instance](/en/miniapp/develop/miniapp/api/tyml/IntersectionObserver/IntersectionObserver)(supports chaining)

### Referenced Types

##### `type` Margins

| Property | Type | Description |
| --- | --- | --- |
| `left` | `number` | Left boundary |
| `right` | `number` | Right boundary |
| `top` | `number` | Top boundary |
| `bottom` | `number` | Bottom boundary |


### Examples

#### Listen for an element's intersection with the viewport

```html
// index.tyml
<scroll-view scroll-y="{{true}}" class="container">
  <view class="placeholder">Scroll down to see the effect</view>
  <view class="target">Target element</view>
</scroll-view>
<view class="status">Intersection ratio: {{ratio}}</view>
```

```js
// index.js
Page({
  data: {
    ratio: 0,
  },
  onReady() {
    var observer = ty.createIntersectionObserver(this, {
      thresholds: [0, 0.5, 1],
    });
    observer
      .relativeToViewport()
      .observe('.target', (res) => {
        this.setData({ ratio: res.intersectionRatio });
        console.log('Intersection ratio', res.intersectionRatio);
        console.log('Target position', res.boundingClientRect);
        console.log('Reference area', res.relativeRect);
        console.log('Intersection area', res.intersectionRect);
        console.log('Timestamp', res.time);
      });
  },
});
```

```css
// index.tyss
.container {
  height: 100vh;
}
.placeholder {
  height: 800rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
}
.target {
  height: 300rpx;
  background-color: #e8f4ff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.status {
  position: fixed;
  top: 20rpx;
  right: 20rpx;
  background-color: rgba(0,0,0,0.6);
  color: #fff;
  padding: 10rpx 20rpx;
  border-radius: 8rpx;
}
```
