> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/api/elements/built-in/scroll-view.md.

# `<scroll-view>`

APISummary
基础滚动组件，支持横向和竖向滚动。当其内容区域比其本身可视区域更大时，允许用户滚动以展示更多内容。

## 使用指南

### 横向与纵向滚动

`<scroll-view>` 支持横向和纵向的滚动，通过 `scroll-orientation` 属性来实现。
`<scroll-view>` 固定使用 [linear](/zh/guide/ui/layout/linear-layout.md) 布局，且布局方向由 `scroll-orientation` 属性决定。

**下面是一个示例: scroll-view**

**Entry:** `src/base`
**Bundle:** `dist/base.lynx.bundle` | Web: `dist/base.web.bundle`

```tsx {15,24}
// Copyright 2025 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { VerticalScrollItem } from "../component/scrollItem.jsx";
import { HorizontalScrollItem } from "../component/scrollItem.jsx";

export const App = () => {
  return (
    <view style={{ width: "100%", height: "100%", padding: 10, display: "linear", marginTop: 20 }}>
      <text style={{ fontSize: "20px", fontWeight: "bold", height: "40px", paddingLeft: "10px", marginTop: "10px" }}>
        Horizontal ScrollView Example
      </text>
      <scroll-view
        scroll-orientation="horizontal"
        style={{ width: "calc(100% - 10px)", height: "100px", paddingLeft: "5px", borderRadius: "10px" }}
      >
        {Array.from({ length: 20 }).map((item, index) => <HorizontalScrollItem index={index} />)}
      </scroll-view>
      <text style={{ fontSize: "20px", fontWeight: "bold", height: "40px", paddingLeft: "10px", marginTop: "10px" }}>
        Vertical ScrollView Example
      </text>
      <scroll-view
        scroll-orientation="vertical"
        style={{ width: "100%", height: "100%", paddingLeft: "5px", marginLeft: "5px" }}
      >
        {Array.from({ length: 20 }).map((item, index) => <VerticalScrollItem index={index} />)}
      </scroll-view>
    </view>
  );
};

```



### 滚动事件

使用 [`bindscroll`](#scroll)、[`bindscrolltoupper`](#scrolltoupper) / [`bindscrolltolower`](#scrolltolower) 等事件回调监听滚动进度的变化。

**下面是一个示例: scroll-view**

**Entry:** `src/event`
**Bundle:** `dist/event.lynx.bundle` | Web: `dist/event.web.bundle`

```tsx {16-24}
// Copyright 2025 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from "@lynx-js/react";
import "./index.css";
import { VerticalScrollItem } from "../component/scrollItem.jsx";
const VerticalScrollContainer = () => {
  return (
    <view
      style={{ width: "100%", height: "100%" }}
    >
      <text className="title">ScrollView Example</text>
      <scroll-view
        scroll-orientation="vertical"
        style="width:100%; height: 100%; padding-left: 5px;margin-left:5px"
        bindscroll={(e) => {
          console.log(e.detail);
        }}
        bindscrolltoupper={(e) => {
          console.log(e.detail);
        }}
        bindscrolltolower={(e) => {
          console.log(e.detail);
        }}
      >
        {Array.from({ length: 20 }).map((item, index) => <VerticalScrollItem index={index} />)}
      </scroll-view>
    </view>
  );
};
export default VerticalScrollContainer;
root.render(<VerticalScrollContainer />);

```



### 吸顶能力

作为 `<scroll-view>` 的孩子节点，可以为其设置 `sticky`, 使得孩子节点在滚动到距离 `<scroll-view>` 顶部一定位置后，不会继续跟随内容滚动，而是吸附在距离 `<scroll-view>` 顶部位置处。

**下面是一个示例: scroll-view**

**Entry:** `src/sticky`
**Bundle:** `dist/sticky.lynx.bundle` | Web: `dist/sticky.web.bundle`

```tsx
// Copyright 2025 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from "@lynx-js/react";
import { VerticalScrollItem } from "../component/scrollItem.jsx";
import { StickyItem } from "./stickyItem.jsx";

const VerticalScrollContainer = () => {
  return (
    <view style={{ width: "100%", height: "100%" }}>
      <text className="title">ScrollView Example</text>
      <scroll-view
        scroll-orientation="vertical"
        style={{ width: "100%", height: "100%", paddingLeft: "5px", marginTop: "5px", marginLeft: "5px" }}
      >
        {Array.from({ length: 20 }).map((item, index) => {
          if (index == 2) {
            return <StickyItem index={index} height={100} sticky={true} />;
          }
          return <VerticalScrollItem index={index} />;
        })}
      </scroll-view>
    </view>
  );
};
export default VerticalScrollContainer;
root.render(<VerticalScrollContainer />);

```



:::tip
`sticky`
 只能被设置给 `<scroll-view>`
 的直接子节点。Android 上需要对 `sticky`
 节点添加属性 `flatten={false}`

`<scroll-view>` 的直接子节点只支持 `linear` 与 `sticky`。如果需要更复杂的布局，如子节点自适应撑开，则建议给 `<scroll-view>` 提供一个唯一的子 view，在该唯一的子节点内部实现更健全的 CSS 能力。

```javascript
<scroll-view scroll-orientation="vertical">
  <view> // do any thing you want
  {...}
  </view>
</scroll-view>
```

:::

## 属性

属性名和属性值用于描述元件的行为和外观

### `scroll-orientation`

```ts
// DefaultValue: "vertical"
scroll-orientation?: string
```

设置 `<scroll-view>` 滚动方向。

| 值          | 说明                                           |
| ---------- | -------------------------------------------- |
| vertical   | `<scroll-view>`子节点纵向布局，`<scroll-view>`本身纵向滚动 |
| horizontal | `<scroll-view>`子节点横向布局，`<scroll-view>`本身横向滚动 |

### `enable-scroll`

```ts
// DefaultValue: true
enable-scroll?: boolean
```

设置是否允许手势拖拽滚动。支持动态切换，在下一次手势中生效。禁止滚动时，用户手动不能滚动

| 值     | 说明                                    |
| ----- | ------------------------------------- |
| true  | 用户手势可以触发滚动                            |
| false | 用户手势不可以触发滚动，但仍能被 `scrollTo` 等滚动方法直接滚动 |

### `initial-scroll-offset`

```ts
// DefaultValue: N/A
initial-scroll-offset?: number = ${number}px
```

设置初次渲染时的内容**绝对**距离偏移（与 [`scrollTo`](#scrollto) 方法中的 offset 概念有所区别），横纵向由 `scroll-orientation` 决定，仅在第一次 `render` 执行时起效，不响应后续更改。

:::tip
**与 `initial-scroll-offset/to-index` 会相互覆盖，不能一起使用**
:::

### `initial-scroll-to-index`

```ts
// DefaultValue: N/A
initial-scroll-to-index?: number = ${number}
```

设置初次渲染时定位到的子节点，仅在第一次 `render` 执行时起效，不响应后续更改。

:::tip
**与 `initial-scroll-offset/to-index` 会相互覆盖，不能一起使用**

如果 `index` 非法（如为负值、超出子节点个数），则设置无效
:::

### `bounces`

```ts
// DefaultValue: true
bounces?: boolean
```

开启边缘回弹效果。

### `upper-threshold`

```ts
// DefaultValue: N/A
upper-threshold?: string = ${number}px
```

设置一个滚动阈值 (单位 `px`)，表示距离顶部或者左边多远时触发 `scrolltoupper` 事件。

### `lower-threshold`

```ts
// DefaultValue: N/A
lower-threshold?: string = ${number}px
```

设置一个滚动阈值，表示距离底部或者右边多远时触发 `scrolltolower` 事件。

### `scroll-bar-enable`

```ts
// DefaultValue: false
scroll-bar-enable?: boolean
```

开启滚动条，支持动态切换。

:::tip

Android 仅纵向滚动有滚动条 `iOS`
 支持纵向和横向滚动条 :::
## 事件

前端可以在组件上绑定相应事件回调来监听组件的运行时行为。

### `scroll`

```ts
bindscroll = (e: scrollEvent) => {};

interface scrollEvent extends CustomEvent {
  detail: {
    /**
     * 事件名称
     **/
    type: 'scroll';
    /**
     * 距上次滚动的横向滚动偏移量
     **/
    deltaX: number;
    /**
     * 距上次滚动的纵向滚动偏移量
     **/
    deltaY: number;
    /**
     * 当前横向滚动偏移量
     **/
    scrollLeft: number;
    /**
     * 当前纵向滚动偏移量
     **/
    scrollTop: number;
    /**
     * 当前内容区域高度
     **/
    scrollHeight: number;
    /**
     * 当前内容区域宽度
     **/
    scrollWidth: number;
  };
}
```

发生滚动时（有/无动画），触发本事件。

:::tip

如果有回弹 (`bounces={true}`)，则在回弹状态下`scrollLeft`与`scrollTop`可能为负。
:::

### `scrolltoupper`

```ts
bindscrolltoupper = (e: scrollToUpperEvent) => {};

interface scrollToUpperEvent extends CustomEvent {
  detail: {
    /**
     * 事件名称
     **/
    type: 'scrolltoupper';
    /**
     * 默认值
     **/
    deltaX: 0;
    /**
     * 默认值
     **/
    deltaY: 0;
  };
}
```

当滚动区域的上/左边的 [`upperThreshold`](#upper-threshold) 规定的区域与可视区域相交时，触发该事件。

### `scrolltolower`

```ts
bindscrolltolower = (e: scrollToLowerEvent) => {};
interface bindscrolltolower extends CustomEvent {
  detail: {
    /**
     * 事件名称
     **/
    type: 'scrolltolower';
    /**
     * 默认值
     **/
    deltaX: 0;
    /**
     * 默认值
     **/
    deltaY: 0;
  };
}
```

当滚动区域的上/左边的 [`lowerThreshold`](#lower-threshold) 规定的区域与可视区域相交时，触发该事件。

### `scrollend`

```ts
bindscrollend = (e: scrollEndEvent) => {};
interface scrollEndEvent extends CustomEvent {
  detail: {
    /**
     * 事件名称
     **/
    type: 'scrollend';
    /**
     * 默认值
     **/
    deltaX: 0;
    /**
     * 默认值
     **/
    deltaY: 0;
    /**
     * 当前横向滚动偏移量
     **/
    scrollLeft: number;
    /**
     * 当前纵向滚动偏移量
     **/
    scrollTop: number;
    /**
     * 当前内容区域高度
     **/
    scrollHeight: number;
    /**
     * 当前内容区域宽度
     **/
    scrollWidth: number;
  };
}
```

滚动结束时触发该事件。

### `contentsizechanged`

```ts
bindcontentsizechanged = (e: contentSizeChanged) => {};

interface contentSizeChanged extends CustomEvent {
  detail: {
    /**
     * 事件名称
     **/
    type: 'contentsizechanged';
    /**
     * 新内容区域宽度
     **/
    scrollWidth: number;
    /**
     * 新内容区域高度
     **/
    scrollHeight: number;
  };
}
```

直接子节点组成的内容区域的宽高发生变化时，触发本事件。
本事件会在 `<scroll-view>` 的内容完成 layout 后触发。如对 `<scroll-view>` 的子节点进行更新，请在该事件中调用更新后的 [`scrollTo`](#scrollto) 等滚动方法。

## 方法

前端可以通过 [SelectorQuery](/zh/api/lynx-api/nodes-ref/nodes-ref-invoke.md) API 执行组件的方法，使用方法如下。

```ts title=index.js
lynx
  .createSelectorQuery() // 创建 SelectorQuery
  .select('#video') // 指定目标节点的选择器
  .invoke({
    // 指定对目标节点的操作
    method: 'seekTo',
    params: {
      duration: 1000,
    },
    success: function (res) {
      console.log(res);
    },
    fail: function (res) {
      console.log(res.code, res.data);
    },
  })
  .exec(); // 执行查询
```

### `scrollTo`

```tsx
<scroll-view id="scroll" />
// 目标滚动位置的计算方法：MIN(可滚动区域，child(index).position + offset)
lynx.createSelectorQuery()
  .select(`#scroll`)
  .invoke({
    method: 'scrollTo',
    params: {
      offset: 0, // offset 设置内容偏移量绝对值
      index: 1, // index 目标子节点，如果设置为零，则会滚动 list 的 padding 的距离
      smooth: true // smooth 设置是否平滑滚动
    },
  })
  .exec();
```

将 `<scroll-view>` 的内容定位到特定位置，可以使用子节点序号 `index` 或绝对定位 `offset`。以 `offset` 为参数时，向右为正，向左为负；`RTL` 模式下正负会被倒转，向左为正，向右为负。

### `autoScroll`

```tsx
<scroll-view id="scroll" />

lynx.createSelectorQuery()
  .select(`#scroll`)
  .invoke({
     method: 'autoScroll',
      params: {
        rate: 120, // rate 滑动速度，每秒的距离(单位：px/sec)
        start: true // start 设置开始或者停止自动滚动
      },
  })
  .exec();
```

触发自动滚动。如果 `rate` 过小会无法滑动。滚动到边界后，会主动停止 `autoScroll`。如果用户重新反向拖拽到某一个位置，松手后, `autoScroll` 也不会重新触发自动滚动。

### `scrollIntoView`

```tsx
<scroll-view>
  <text id="targetnode">"click me, scrollIntoView"</text>
</scroll-view>

lynx.createSelectorQuery()
  .select(`#targetnode`)
  .invoke({
    method: 'scrollIntoView',
    params: {
      scrollIntoViewOptions: {
        block: 'center', // 纵向对齐方式: “start" 顶对齐 | "center" 居中对齐 | "end" 底对齐
        inline: 'start', // 横向对齐方式： "start" 左对齐 | "center" 居中对齐 | "end" 右对齐
        behavior: 'smooth', // 'smooth', // "smooth" | "none" 可选，指顶滚动是否带有动画
      },
    },
  })
  .exec();
```

使 `<scroll-view>` 滚动到指定子节点。本方法绑定在 `<scroll-view>` 的任意（直接/非直接）子节点上，而不是 `<scroll-view>` 本身。

### `scrollBy`

```tsx
<scroll-view id="scrollview" />

lynx.createSelectorQuery()
  .select('#scrollview')
  .invoke({
    method: 'scrollBy',
    params: {
      offset: 100, // 滚动的距离, 单位 px
    },
    success(res) {
      console.log('succ ');
    },
    fail(res) {
      console.log('err ');
    },
}).exec();
```

在现有偏移量基础上滚动的距离，单位 `px`

## 性能优化建议

`<scroll-view>` 会一次性创建所有的子节点，这有可能导致首屏耗时非常严重。可以通过曝光事件驱动它仅创建可见的子节点。

`<scroll-view>` 没有任何复用机制，如果其内容过多，可能会占用非常大的问题，导致 `OOM` 等稳定性问题。

数据超过 3 屏时，尽可能的使用 `<list>` 来优化性能，或者根据曝光事件自行模拟 `<VisualizedList>` 逻辑。

## 兼容性

**Compatibility Table**
**Query:** `elements.scroll-view`

**Platform Support**

| Platform | Version Added | Notes |
|----------|---------------|-------|
| Android | 1.5 | - |
| iOS | 1.5 | - |
| HarmonyOS | 3.4 | - |
| Clay Android | 1.5 | - |
| Clay iOS | 1.5 | - |
| Clay Windows | 1.5 | - |
| Clay macOS | 1.5 | - |
| Web | ✅ Yes | - |

**Description:** scroll-view

