> For the complete documentation index, see [llms.txt](https://docs.horizon.ipaper.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.horizon.ipaper.io/horizon-api/methods-and-events/viewport.md).

# Viewport

### `getViewportState`

Get the viewport state as is its right now. Returns an `IViewportState`.

```javascript
const viewportState = api.viewport.getViewportState();
console.log('Viewport State ', viewportState );
```

`IViewportState`

<table><thead><tr><th width="307">Property</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>percentInView</td><td><code>{</code> <br>   <code>start: number,</code> <br>   <code>end: number</code> <br><code>}</code></td><td>percentages in view at start and end of viewport</td></tr><tr><td>sections</td><td><code>ILeafletSection[]</code></td><td>sections in the viewport</td></tr><tr><td>pages</td><td><code>ILeafletPage[]</code></td><td>pages in the viewport</td></tr><tr><td>content</td><td><code>ILeafletContent[]</code></td><td>content in the viewport</td></tr></tbody></table>

`ILeafletSection`

<table><thead><tr><th width="307">Property</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>$type</td><td><code>'section'</code></td><td>content type</td></tr><tr><td>sectionGuid</td><td><code>string</code></td><td>the unique section guid</td></tr><tr><td>title</td><td><code>string</code> (optional)</td><td>title of the section</td></tr></tbody></table>

`ILeafletPage`

<table><thead><tr><th width="307">Property</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>$type</td><td><code>'page'</code></td><td>content type</td></tr><tr><td>pageGuid</td><td><code>string</code></td><td>the unique page guid</td></tr><tr><td>pageNumber</td><td><code>number</code></td><td>the page number</td></tr></tbody></table>

`ILeafletContent`

| Property    | Type                                                         | Description                                 |
| ----------- | ------------------------------------------------------------ | ------------------------------------------- |
| $type       | `'offer'` \| `'text'` \| `'image'` \| `'video'` \| `'scene'` | content type                                |
| contentGuid | `string`                                                     | guid of content                             |
| \[key]      | `[type]`                                                     | additional properties based on content type |

Currently, only the `offer` content type has additional properties:

`ILeafletOffer`

| Property     | Type                | Description          |
| ------------ | ------------------- | -------------------- |
| header       | `string`            | header of offer      |
| summary      | `string`            | summary of offer     |
| sales\_price | `{ value: number }` | sales price of offer |
| products     | `ILeafletProduct[]` | products on offer    |

`ILeafletProduct`

| Property    | Type                | Description           |
| ----------- | ------------------- | --------------------- |
| $type       | `'product'`         | content type          |
| sku         | `string`            | sku of product        |
| title       | `string`            | title of product      |
| url         | `string`            | url of product        |
| price       | `{ value: number }` | price of product      |
| sale\_price | `{ value: number }` | sale price of product |
| brand       | `string`            | product brand         |

***

## Subscription events

The following events can be subscribed to in order to execute code when they occur. The code that should be executed is passed as a callback function when subscribing. You can set up multiple subscriptions to the same event.

These can be very useful fx. for tracking with Google Analytics, etc.

### `subscribe`

Subscribe to chapters entering the viewport.

```javascript
api.viewport.subscribe((viewportEvent) => {
    // your code
});
```

`viewportEvent`

| Property | Type                     | Description                           |
| -------- | ------------------------ | ------------------------------------- |
| action   | `'scroll'` \| `'resize'` | name of the event action that occured |
| source   | `undefined`              | property not in use                   |
| value    | `IViewportState`         | the current viewport state            |

`IViewportState`

See `getViewportState` at the top of the page for definition.

### `unsubscribe`

In addition, when you set up a subscription you will get an `unsubscribe` function returned which can be used to cancel the subscription at any time. For example:

```javascript
let mySubscription = api.viewport.subscribe((viewportEvent) => {
    // your code
});

mySubscription.unsubscribe();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.horizon.ipaper.io/horizon-api/methods-and-events/viewport.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
