目录

scroll-view

Represents a scrollable view area, equivalent to a div element with its overflow property set in web development.

Since: 1.0.0

Constraints:

When enabling vertical scrolling, you need to set height through CSS to provide a fixed height for scroll-view. The default length unit of the component properties is px.

Attributes

Name Type Default Value Mandatory Description
enhanced boolean false No

Whether to enable the enhanced feature of scroll-view. After this feature is enabled, you can use ScrollViewContext to operate scroll-view.

Since: 1.0.23

scroll-x boolean false No Whether to allow horizontal scrolling.
scroll-y boolean false No Whether to allow vertical scrolling.
upper-threshold number|string 50 No Distance from the top or left edge to trigger the scrolltoupper event.
lower-threshold number|string 50 No Distance from the bottom or right edge to trigger the scrolltolower event.
scroll-top number|string 0 No Position of the vertical scrollbar.
scroll-left number|string 0 No Position of the horizontal scrollbar.
scroll-into-view string - No ID of the sub-element to scroll to. The value cannot start with a digit. The view scrolls to that element in the direction specified as scrollable.
scroll-into-view-offset number 0 No

Additional offset when jumping to the target node of scroll-into-view.

Since: 2.0.1

scroll-with-animation boolean false No Whether to use animation transitions when setting the scrollbar position.
show-scrollbar boolean true No

Scrollbar visibility control (without any other restrictions) in versions earlier than 2.0.1.

Scrollbar visibility control (effective only when the enhanced attribute is enabled) in versions later than 2.0.1.

refresher-enabled boolean false No Whether to enable the custom pull-to-refresh feature.
refresher-threshold number 45 No Custom pull-to-refresh threshold.
refresher-default-style string "black" No Default pull-to-refresh style. The value can be black, white, or none (default style not used).
refresher-background string transparent No Background color of the custom pull-to-refresh area. The default color is transparent.
refresher-triggered boolean false No Pull-to-refresh status. The value true indicates that the pull-to-refresh has been triggered, while false indicates it has not.
bounces boolean true No

Boundary elasticity control (effective only when the enhanced attribute is enabled).

Since: 2.0.1

fast-deceleration boolean false No

Swiping deceleration rate control (effective only when the enhanced attribute is enabled).

Since: 2.0.1

scroll-anchoring boolean false No

Scroll anchoring feature, which prevents the scrolling position from jittering when the content changes.

Since: 2.0.1

enable-flex boolean false No

Feature for enabling the flexbox layout. After this feature is enabled, if the current node declares display: flex, it becomes a flex container and applies to its child nodes.

Since: 2.0.1

paging-enabled boolean false No

Feature for enabling pagination scrolling (effective only when the enhanced attribute is enabled).

Since: 2.0.1

using-sticky boolean false No

Feature for enabling the position: sticky attribute in the scroll-view component.

Since: 2.0.1

Customizing Content for Pull-to-Refresh

When refresher-default-style is set to none, declare the node of slot="refresher" under [/topic/body/section/p/b/scroll-view {""}) (scroll-view] to implement custom pull-to-refresh content. For details, see the following example.

Since: 1.0.23

Events

Name Parameter Description
bindscrolltoupper eventhandle Event triggered when scrolling to the top or left edge.
bindscrolltolower eventhandle Event triggered when scrolling to the bottom or right edge.
bindscroll eventhandle Event triggered when scrolling.
binddragstart eventhandle

Event triggered when the swiping starts (effective only when the enhanced attribute is enabled).

Since: 2.0.1

binddragging eventhandle

Event triggered when swiping (effective only when the enhanced attribute is enabled).

Since: 2.0.1

binddragend eventhandle

Event triggered when the swiping ends (effective only when the enhanced attribute is enabled).

Since: 2.0.1

bindrefresherpulling eventhandle Event triggered when the pull-to-refresh component is pulled down.
bindrefresherrefresh eventhandle Event triggered when the pull-to-refresh component is activated.
bindrefresherrestore eventhandle Event triggered when the pull-to-refresh component is restored.
bindrefresherabort eventhandle Event triggered when the pull-to-refresh component is aborted.

Example:

  1. Example of the scroll-view component:

    HXML file:

    <scroll-view
      scroll-y="{{scrollY}}"
      style="height: 50px"
      upper-threshold="50"
      lower-threshold="30px"
      bindscrolltoupper="scrollToUpperEvent"
      bindscrolltolower="scrollToLowerEvent"
      bindscroll="scrollEvent"
      bindrefresherpulling="refresherPullingEvent"
      bindrefresherrefresh="refresherRefreshEvent"
      bindrefresherrestore="refresherRestoreEvent"
      bindrefresherabort="refresherAbortEvent">
      <view id="demo1" class="scroll-view-item demo-text-1">1</view>
      <view id="demo2" class="scroll-view-item demo-text-2">2</view>
      <view id="demo3" class="scroll-view-item demo-text-3">3</view>
    </scroll-view>
    <view>Customize a pull-to-refresh event.</view>
    <scroll-view
      class="page-section page-section-gap"
      scroll-y="{{scrollY}}"
      refresher-enabled="{{true}}"
      style="height: 500px"
      upper-threshold="50"
      lower-threshold="30px"
      bindscrolltoupper="scrollToUpperEvent"
      bindscrolltolower="scrollToLowerEvent"
      bindscroll="scrollEvent"
      bindrefresherpulling="refresherPullingEvent"
      bindrefresherrefresh="refresherRefreshEvent"
      bindrefresherrestore="refresherRestoreEvent"
      bindrefresherabort="refresherAbortEvent">
      <view id="demo1" class="scroll-view-item demo-text-1">3</view>
      <view id="demo2" class="scroll-view-item demo-text-2">4</view>
      <view id="demo3" class="scroll-view-item demo-text-3">5</view>
    </scroll-view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        height: 50
      },
      scrollToUpperEvent(event) {
        // Example of event.detail: {"direction":""}
        console.info('Event triggered when the scroll-view component scrolls to the top or left edge. Carried value: ', event.detail);
      },
      scrollToLowerEvent(event) {
        // Example of event.detail: {"direction":""}
        console.info('Event triggered when the scroll-view component scrolls to the bottom or right edge. Carried value: ', event.detail);
      },
      scrollEvent(event) {
        // Example of event.detail: {"scrollTop":0,"scrollLeft":0,"scrollWidth":0,"scrollHeight":0,"deltaY":0,"deltaX":0}.
        console.info('Event triggered when the scroll-view component scrolls. Carried value: ', event.detail);
      },
      refresherPullingEvent(event) {
        // Example of event.detail: {"dy":0}
        console.info('Event triggered when the custom pull-to-refresh component of the scroll-view component is pulled down. Carried value: ', event.detail);
      },
      refresherRefreshEvent(event) {
        // Example of event.detail: {"dy":0}
        console.info ('Event triggered when the custom pull-to-refresh component of the scroll-view component is activated. Carried value: ', event.detail);
      },
      refresherRestoreEvent(event) {
        // Example of event.detail: {"dy":0}
        console.info('Event triggered when the custom pull-to-refresh component of the scroll-view component is restored. Carried value: ', event.detail);
      },
      refresherAbortEvent(event) {
        // Example of event.detail: {"dy":0}
        console.info('Event triggered when the custom pull-to-refresh component of the scroll-view component is aborted. Carried value: ', event.detail);
      }
    });
  2. Example of custom pull-to-refresh content:

    HXML file:

    <view>
      <scroll-view
        style="width: 100%; height: 100vh"
        scroll-y="{{scrollY}}"
        refresher-enabled="{{refresherEnabled}}"
        refresher-threshold="{{refresherThreshold}}"
        refresher-default-style="{{refresherDefaultStyle}}"
        refresher-triggered="{{refresherTriggered}}"
        bindrefresherrefresh="refresherRefreshEvent">
        <view slot="refresher" class="custom-refresher">
          <view class="refresher-text">
            <text>Pulling down to refresh...</text>
          </view>
        </view>
        <view>
          <view class="scroll-view-item">List item 1</view>
          <view class="scroll-view-item">List item 2</view>
          <view class="scroll-view-item">List item 3</view>
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        refresherEnabled: true,
        refresherThreshold: 45,
        refresherDefaultStyle: 'none',
        refresherTriggered: false
      },
      timeoutId: null,
    
      refresherRefreshEvent() {
        this.setData({
          refresherTriggered: true
        });
        if (this.timeoutId) {
          clearTimeout(this.timeoutId);
        }
        // Simulate content update.
        this.timeoutId = setTimeout(() => {
          this.setData({
            refresherTriggered: false
          });
          this.timeoutId = null;
        }, 2000);
      }
    });

    CSS file:

    .custom-refresher {
        width: 100%;
        height: 45px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    }
    
    .refresher-text {
        font-size: 28rpx;
        color: white;
        font-weight: 500;
    }
    
    .scroll-view-item {
        padding: 50rpx;
    }
  3. Example of boundary elasticity:

    HXML file:

    <view>
      <scroll-view
        style="width: 100%; height: 200px"
        scroll-y="{{scrollY}}"
        bounces="{{bounces}}"
        enhanced="{{enhanced}}">
        <view>
          <view class="scroll-view-item">List item 1</view>
          <view class="scroll-view-item">List item 2</view>
          <view class="scroll-view-item">List item 3</view>
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        bounces: true,
        enhanced: true
      }
    });

    CSS file:

    .scroll-view-item {
        height: 100px;
        background-color: aliceblue;
    }
  4. Example of a swiping event:

    HXML file:

    <view>
      <scroll-view
        style="width: 100%; height: 200px"
        scroll-y="{{scrollY}}"
        enhanced="{{enhanced}}"
        binddragstart="onDragStart"
        binddragging="onDragging"
        binddragend="onDragEnd">
        <view>
          <view class="scroll-view-item">List item 1</view>
          <view class="scroll-view-item">List item 2</view>
          <view class="scroll-view-item">List item 3</view>
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        enhanced: true
      },
      onDragStart(event) {
        // Example of event.detail: {"scrollTop":"","scrollLeft":""}.
        console.info('Event triggered when the scroll-view component starts to scroll. Carried value: ', event.detail);
      },
      onDragging(event) {
        // Example of event.detail: {"scrollTop":"","scrollLeft":""}.
        console.info('Event triggered when the scroll-view component is scrolling. Carried value: ', event.detail);
      },
      onDragEnd(event) {
        // Example of event.detail: {"scrollTop":"","scrollLeft":"","velocity":{"x":"","y":""}}.
        console.info('Event triggered when the scroll-view component stops scrolling. Carried value: ', event.detail);
      }
    });

    CSS file:

    .scroll-view-item {
        height: 100px;
        background-color: aliceblue;
    }
  5. Example of quick deceleration:

    HXML file:

    <view>
      <scroll-view
        style="width: 100%; height: 200px"
        scroll-y="{{scrollY}}"
        fast-deceleration="{{fastDeceleration}}"
        enhanced="{{enhanced}}">
        <view>
          <view class="scroll-view-item">List item 1</view>
          <view class="scroll-view-item">List item 2</view>
          <view class="scroll-view-item">List item 3</view>
          <view class="scroll-view-item">List item 4</view>
          <view class="scroll-view-item">List item 5</view>
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        fastDeceleration: true,
        enhanced: true
      }
    });

    CSS file:

    .scroll-view-item {
        height: 100px;
        background-color: aliceblue;
    }
  6. Flex layout example:

    HXML file:

    <view>
      <scroll-view
        class="flex-scroll-view"
        scroll-y="{{scrollY}}"
        enable-flex="{{enableFlex}}"
        style="display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center">
        <view id="{{'item-' + index}}" class="flex-item" has:for="{{itemCount}}" has:key="index">
            {{index+1}}
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        enableFlex: true,
        itemCount: 30
      }
    });

    CSS file:

    .flex-scroll-view {
        width: 100%;
        height: 300px;
        border: 1px solid #ccc;
        margin-bottom: 20px;
    }
    
    .flex-item {
        width: 75px;
        height: 60px;
        background: linear-gradient(135deg, #80d5cd 0%, #f19e9e 100%);
        color: #ffffff;
        text-align: center;
        line-height: 60px;
        margin: 5px;
        border-radius: 10px;
        flex-shrink: 0;
    }
  7. Example of jumping to the target position:

    HXML file:

    <view>
      <scroll-view
        style="width: 100%; height: 200px"
        scroll-y="{{scrollY}}"
        scroll-into-view="{{scrollIntoView}}"
        scroll-into-view-offset="{{scrollIntoViewOffset}}">
        <view>
          <view class="scroll-view-item">List item 1</view>
          <view id="item2" class="scroll-view-item">List item 2</view>
          <view class="scroll-view-item">List item 3</view>
          <view class="scroll-view-item">List item 4</view>
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        scrollIntoView: '',
        scrollIntoViewOffset: 50
      },
      onLoad() {
        setTimeout(() => {
          this.setData({
            scrollIntoView: 'item2'
          });
        }, 2000);
      }
    });

    CSS file:

    .scroll-view-item {
        height: 100px;
        background-color: aliceblue;
    }
  8. Example of pagination scrolling:

    HXML file:

    <view>
      <scroll-view
        style="width: 100%; height: 200px"
        scroll-y="{{scrollY}}"
        paging-enabled="{{pagingEnabled}}"
        enhanced="{{enhanced}}">
        <view>
          <view class="scroll-view-item">Page 1</view>
          <view class="scroll-view-item">Page 2</view>
          <view class="scroll-view-item">Page 3</view>
          <view class="scroll-view-item">Page 4</view>
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        pagingEnabled: true,
        enhanced: true
      }
    });

    CSS file:

    .scroll-view-item {
        height: 200px;
        background-color: aliceblue;
    }
  9. Example of the sticky feature:

    HXML file:

    <view>
      <scroll-view
        style="width: 100%; height: 300px"
        scroll-y="{{scrollY}}"
        using-sticky="{{usingSticky}}">
        <view>
          <view id="item1" class="scroll-view-item">List item 1</view>
          <view id="item2" class="scroll-view-item">List item 2</view>
          <view id="item3" class="scroll-view-item">List item 3</view>
          <view id="item4" class="scroll-view-item">List item 4</view>
          <view id="item5" class="scroll-view-item">List item 5</view>
          <view id="item6" class="scroll-view-item">List item 6</view>
          <view id="item7" class="scroll-view-item">List item 7</view>
        </view>
      </scroll-view>
    </view>

    JavaScript file:

    Page({
      data: {
        scrollY: true,
        usingSticky: true
      }
    });

    CSS file:

    .scroll-view-item {
        height: 100px;
        background-color: aliceblue;
    }
    
    #item2 {
        position: sticky;
        top: 0;
        background-color: #fee2e2;
    }
    
    #item4 {
        position: sticky;
        bottom: 0;
        background-color: #fee2e2;
    }