---
name: "createIntersectionObserver"
mode: "api"
versionRequirements:
  - { name: "Base Library", version: "2.10.0" }
title: "ty.createIntersectionObserver - Create and return an IntersectionObserver object instance. In custom components or pages containing custom components, use `this.createIntersectionObserver([options])` instead."
---

## createIntersectionObserver

> [VERSION] Base Library >= 2.10.0

### Description

Create and return an IntersectionObserver object instance. Used to determine whether certain nodes are visible to the user and what proportion is visible

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `component` | `any` | Yes | Custom component instance; if omitted, the current page is used by default |
| `options` | `any` | No | Configuration options |

### Return Value

None


### Examples

#### Create an IntersectionObserver to observe element visibility

```html
// index.tyml
<scroll-view scroll-y="{{true}}" class="container">
  <view class="placeholder">Scroll down</view>
  <view class="target">Target</view>
</scroll-view>
<view class="status">{{visible ? 'Visible' : 'Not visible'}}</view>
```

```js
// index.js
Page({
  data: {
    visible: false,
    observer: null,
  },
  onReady() {
    var observer = ty.createIntersectionObserver(this, {
      thresholds: [0, 0.5, 1],
    });
    observer.relativeToViewport().observe('.target', (res) => {
      this.setData({ visible: res.intersectionRatio > 0 });
      if (res.intersectionRatio > 0) {
        console.log('Element visible');
      } else {
        console.log('Element not visible');
      }
    });
    this.data.observer = observer;
  },
  onUnload() {
    if (this.data.observer) {
      this.data.observer.disconnect();
    }
  },
});
```

```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;
}
```
