鸿蒙应用开发之通过Scroll、nestedScroll实现京东秒杀嵌套滚动效果

通过Scroll、nestedScroll实现京东秒杀嵌套滚动效果

通过Scroll、nestedScroll实现京东秒杀嵌套滚动效果

当上拉时,外层先滚动慢慢的黄色签到图片会消失、然后List列表可以继续上拉

效果预览:

实现思路:

  1. 实现页面结构布局,红、黄、绿、列表
scss 复制代码
@Entry
@Component
struct Index {
  build() {
    Column() {
      // 红色(不要放到Column   因为后续下面黄色、蓝、列表需要滚动  而这个红色不需要)
      Text().width('100%').height(150).backgroundColor(Color.Red)
      // 下述后续要上拉滚动
      Column() {
        // 黄色签到
        Text().width('100%').height(100).backgroundColor(Color.Yellow)
        // 蓝色 
        Text().width('100%').height(50).backgroundColor(Color.Blue)
        // 列表
        List() {
          ForEach('0123456789abcdefg'.split(''), (item:string) => {
            ListItem() {
              Text(item).height(50).fontSize(30)
            }
          })
        }.layoutWeight(1).divider({strokeWidth:2,color:Color.Red})
      }
    }
  }
}
  1. 实现黄色、蓝色、列表上拉滚动 通过Scroll容器
scss 复制代码
@Entry
@Component
struct Index {
  build() {
    Column() {
      // 红色
      Text().width('100%').height(150).backgroundColor(Color.Red)
      // 核心1⭐️:多层嵌套滚动
      Scroll() {
        Column() {
          // 黄色
          Text().width('100%').height(100).backgroundColor(Color.Yellow)
          // 蓝色
          Text().width('100%').height(50).backgroundColor(Color.Blue)
          // 列表
          List() {
            ForEach('0123456789abcdefg'.split(''), (item:string) => {
              ListItem() {
                Text(item).height(50).fontSize(30)
              }
            })
          }.divider({strokeWidth:2,color:Color.Red})
        }
      }.layoutWeight(1)
    }
  }
}
  1. 实现上拉黄色慢慢消失,蓝色吸顶效果
scss 复制代码
@Entry
@Component
struct Index {
  build() {
    Column() {
      // 红色
      Text().width('100%').height(150).backgroundColor(Color.Red)
      // 核心1⭐️:多层嵌套滚动
      Scroll() {
        Column() {
          // 黄色
          Text().width('100%').height(100).backgroundColor(Color.Yellow)
          // 蓝色
          Text().width('100%').height(50).backgroundColor(Color.Blue)
          // 列表
          List() {
            ForEach('0123456789abcdefg'.split(''), (item:string) => {
              ListItem() {
                Text(item).height(50).fontSize(30)
              }
            })
          }.divider({strokeWidth:2,color:Color.Red})
          // 核心2⭐:上拉黄色慢慢消失、蓝色一直在  也就是减去蓝色高度
          .height('calc(100% - 50vp)')
        }
      }.layoutWeight(1)
    }
  }
}
  1. 通过nestedScroll属性实现外层滚动Scroll、内层List滚动控制
scss 复制代码
@Entry
@Component
struct Index {
  build() {
    Column() {
      // 红色
      Text().width('100%').height(150).backgroundColor(Color.Red)
      // 核心1⭐️:多层嵌套滚动
      Scroll() {
        Column() {
          // 黄色
          Text().width('100%').height(100).backgroundColor(Color.Yellow)
          // 蓝色
          Text().width('100%').height(50).backgroundColor(Color.Blue)
          // 列表
          List() {
            ForEach('0123456789abcdefg'.split(''), (item:string) => {
              ListItem() {
                Text(item).height(50).fontSize(30)
              }
            })
          }.divider({strokeWidth:2,color:Color.Red})
          // 核心2⭐:上拉黄色慢慢消失、蓝色一直在  也就是减去蓝色高度
          .height('calc(100% - 50vp)')
          // 核心3⭐️:向前向后滚动模式 -> 实现与父组件的滚动联动
          .nestedScroll({
            scrollForward: NestedScrollMode.PARENT_FIRST, // 上拉
            scrollBackward: NestedScrollMode.SELF_FIRST   // 下拉
            // 不管父-SELF_ONLY、SELF_FIRST-到边缘管父、PARENT_FIRST-到边缘管子、PARALLEL-父子同时
            // 详细 https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/ts-appendix-enums-V13#nestedscrollmode10
          })
        }
      }.layoutWeight(1)
    }
  }
}

完整代码:

scss 复制代码
@Entry
@Component
struct Index {
  build() {
    Column() {
      // 红色
      Text().width('100%').height(150).backgroundColor(Color.Red)
      // 核心1⭐️:多层嵌套滚动
      Scroll() {
        Column() {
          // 黄色
          Text().width('100%').height(100).backgroundColor(Color.Yellow)
          // 蓝色
          Text().width('100%').height(50).backgroundColor(Color.Blue)
          // 列表
          List() {
            ForEach('0123456789abcdefg'.split(''), (item:string) => {
              ListItem() {
                Text(item).height(50).fontSize(30)
              }
            })
          }.divider({strokeWidth:2,color:Color.Red})
          // 核心2⭐:上拉黄色慢慢消失、蓝色一直在  也就是减去蓝色高度
          .height('calc(100% - 50vp)')
          // 核心3⭐️:向前向后滚动模式 -> 实现与父组件的滚动联动
          .nestedScroll({
            scrollForward: NestedScrollMode.PARENT_FIRST, // 上拉
            scrollBackward: NestedScrollMode.SELF_FIRST   // 下拉
            // 不管父-SELF_ONLY、SELF_FIRST-到边缘管父、PARENT_FIRST-到边缘管子、PARALLEL-父子同时
            // 详细 https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/ts-appendix-enums-V13#nestedscrollmode10
          })
        }
      }.layoutWeight(1)
    }
  }
}

鸿蒙开发者班级

相关推荐
小雨下雨的雨2 小时前
Flutter 框架跨平台鸿蒙开发 —— SingleChildScrollView 控件之长内容滚动艺术
flutter·ui·华为·harmonyos·鸿蒙
2501_944521002 小时前
rn_for_openharmony商城项目app实战-商品评价实现
javascript·数据库·react native·react.js·ecmascript·harmonyos
lili-felicity2 小时前
React Native for Harmony 企业级 Grid 宫格组件 完整实现
react native·react.js·harmonyos
lili-felicity5 小时前
React Native 鸿蒙跨平台开发:动态表单全场景实现
react native·harmonyos
奋斗的小青年!!5 小时前
Flutter跨平台开发适配OpenHarmony:文件系统操作深度实践
flutter·harmonyos·鸿蒙
奋斗的小青年!!6 小时前
Flutter跨平台开发OpenHarmony应用:个人中心实现
开发语言·前端·flutter·harmonyos·鸿蒙
IT=>小脑虎7 小时前
鸿蒙开发零基础小白学习知识点【基础版·详细版】
学习·华为·harmonyos
2501_948122638 小时前
React Native for OpenHarmony 实战:Steam 资讯 App 个人中心页面
javascript·react native·react.js·游戏·ecmascript·harmonyos
水手冰激淋8 小时前
rn_for_openharmony狗狗之家app实战-领养详情实现
harmonyos
粲然忧生9 小时前
腾讯云终端性能监控SDK正式上线,为鸿蒙开发适配保驾护航
android·腾讯云·harmonyos