【鸿蒙5.0】Scroll左右滑动

效果图

运行效果图

代码如下

复制代码
import { curves } from '@kit.ArkUI'

@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller()
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  build() {
    Stack({ alignContent: Alignment.TopStart }) {
      Column(){
        Scroll(this.scroller) {
          Row() {
            ForEach(this.arr, (item: number) => {
              Text(item.toString())
                .fontColor(Color.Orange)
                .width(150)
                .height('90%')
                .backgroundColor(0xFFFFFF)
                .borderRadius(15)
                .fontSize(16)
                .textAlign(TextAlign.Center)
                .margin({ left: 10 })
            }, (item: string) => item)
          }
          .height('50%')
        }
        .scrollable(ScrollDirection.Horizontal)
        .scrollBar(BarState.On)
        .scrollBarColor(Color.Gray)
        .scrollBarWidth(10)
        .friction(0.6)
        .edgeEffect(EdgeEffect.None)
        .onWillScroll((xOffset: number, yOffset: number, scrollState: ScrollState) => {
          console.info(xOffset + ' ' + yOffset)
        })
        .onScrollEdge((side: Edge) => {
          console.info('To the edge')
        })
        .onScrollStop(() => {
          console.info('Scroll Stop')
        })

        Column({ space: 15 }) {
          Button('scroll 150 右滑指定距离150vp')
            .onClick(() => {
              this.scroller.scrollBy(150, 0)
            })

          Button('scroll 100')
            .onClick(() => {
              const xOffset: number = this.scroller.currentOffset().xOffset;
              this.scroller.scrollTo({ xOffset: xOffset + 100, yOffset: 0 })
            })

          Button('scroll 100 with animation')
            .onClick(() => {
              let curve = curves.interpolatingSpring(10, 1, 228, 30)
              const xOffset: number = this.scroller.currentOffset().xOffset;
              this.scroller.scrollTo({
                xOffset: xOffset + 100,
                yOffset: 0,
                animation: { duration: 1000, curve: curve }
              })
            })

          Button('back to start')
            .onClick(() => {
              this.scroller.scrollEdge(Edge.Start)
            })

          Button('next page')
            .onClick(() => {
              this.scroller.scrollPage({ next: true, animation: true })
            })

          Button('fling -3000')
            .onClick(() => {
              this.scroller.fling(-3000)
            })

          Button('scroll to end')
            .onClick(() => {
              this.scroller.scrollEdge(Edge.End, { velocity: 700 })
            })
        }
      }
      .width('100%')
      .height('100%')
      .backgroundColor(0xDCDCDC)
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
  }
}
相关推荐
熬夜敲代码的小N1 小时前
鸿蒙PC开发者必备!GitNext深度测评:一站式Git管理工具
git·华为·harmonyos
秋の本名3 小时前
第一章 鸿蒙生态架构与开发理念
华为·wpf·harmonyos
Ww.xh3 小时前
鸿蒙系统中HTML与Vue集成方案
vue.js·html·harmonyos
前端不太难4 小时前
鸿蒙游戏 CI/CD:为什么你还在手动打包?
游戏·ci/cd·harmonyos
全栈若城4 小时前
HarmonyOS Pen Kit 实战:手写笔轻捏、双击与取色器完整集成
华为·harmonyos·手写笔·harmonyos6
xmdy58665 小时前
Flutter+开源鸿蒙实战|校园易生活Day1 项目初始化搭建+开发环境校验+工程目录规范+第三方库集成+多端屏幕适配+全局底部导航
flutter·开源·harmonyos
想你依然心痛6 小时前
HarmonyOS 6(API 23)实战:打造“空间交互式AR健身私教“——基于Face AR疲劳监测 + Body AR姿态识别的沉浸光感运动系统
ar·restful·harmonyos·悬浮导航·沉浸光感
xmdy58666 小时前
Flutter+开源鸿蒙实战|校园易生活Day2 第三方库批量集成+全局Toast提示+网络状态监听+首页轮播图+资讯卡片布局
flutter·开源·harmonyos
前端不太难7 小时前
为什么说鸿蒙 App 是“状态系统”?
华为·状态模式·harmonyos
SmartBrain7 小时前
Harness 工程建设与 AI 平台建设对比
大数据·人工智能·华为·aigc