HarmonyOS:页面滚动时标题悬浮、背景渐变

一、需求场景

  • 进入到app首页或者分页列表首页时,随着页面滚动,分类tab要求固定悬浮在顶部。
  • 进入到app首页、者分页列表首页、商品详情页时,页面滚动时,顶部导航栏(菜单、标题)背景渐变。

二、相关技术知识点

  • Scroll:可滚动容器,其中nestedScroll:设置父组件的滚动联动、onDidScroll:滚动事件回调
  • Stack:堆叠容器

三、解决方案

  1. 使用Stack层叠布局,将标题栏悬浮展示在页面顶部。
  2. 考虑页面滚动以及tabContent里面的list滚动就要考虑滚动嵌套问题目前场景需要选择:
    向上滚动时,父组件先滚动,父组件滚动到边缘以后自身滚动;
    向下滚动时:自身先滚动,自身滚动到边缘以后父组件滚动。

四、示例

效果图

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

示例代码:TestStickyNestedScroll.ets

import AppStorageConstants from '../../common/AppStorageConstants';@Entry
@Component
struct TestStickyNestedScroll {@State arr: number[] = [];@State opacityNum: number = 0;@State curYOffset: number = 0;@State statusBarHeight: number = 20@State bottomNavBarHeight: number = 20@State navIndicatorHeight: number| undefined = 28aboutToAppear(): void {for (let index = 0; index < 40; index++) {this.arr.push(index);}let tempStatusBarHeight: number | undefined = AppStorage.get(AppStorageConstants.STATUS_BAR_HEIGHT)this.statusBarHeight = tempStatusBarHeight == undefined ? 20 : tempStatusBarHeightthis.navIndicatorHeight = AppStorage.get(AppStorageConstants.NAV_INDICATOR_HEIGHT)// let typeSys = window.AvoidAreaType.TYPE_SYSTEM;// let typeNavIndicator = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR;// window.getLastWindow(getContext(this)).then((data) => {//   // 获取系统默认区域,一般包括状态栏、导航栏//   let avoidArea1 = data.getWindowAvoidArea(typeSys);//   // 顶部状态栏高度//   let orginStatusBarHeight = avoidArea1.topRect.height;//////   this.statusBarHeight = this.getUIContext().px2vp(orginStatusBarHeight);//   console.log("顶部状态栏高度 statusBarHeight = " + this.statusBarHeight + " vp,  orginStatusBarHeight = " +//     orginStatusBarHeight + " px");//   // 部状态栏高度 statusBarHeight = 32.92307692307692 vp,  orginStatusBarHeight = 107 px////   // 底部导航条区域高度//   let avoidArea2 = data.getWindowAvoidArea(typeNavIndicator);//   let orginNavIndicator = avoidArea2.bottomRect.height//   this.navIndicatorHeight = this.getUIContext().px2vp(orginNavIndicator);//   console.log("底部导航条区域高度 navIndicatorHeight = " + this.navIndicatorHeight +//     " vp,  orginNavIndicator = " +//     orginNavIndicator + " px");//   // 底部导航条区域高度 navIndicatorHeight = 28 vp,  orginNavIndicator = 91 px////   //底部导航栏的高度//   let orginBottomStatusBarHeight = avoidArea1.bottomRect.height;//   this.bottomNavBarHeight = this.getUIContext().px2vp(orginBottomStatusBarHeight);//   console.log("底部导航栏的高度 statusBarHeight = " + this.bottomNavBarHeight + " vp,  orginBottomStatusBarHeight = " +//     orginBottomStatusBarHeight + " px");//   // 底部导航栏的高度 statusBarHeight = 0 vp,  orginBottomStatusBarHeight = 0 px// })}@StyleslistCard() {.backgroundColor(Color.White).height(72).width('calc(100% - 20vp)').borderRadius(12).margin({ left: 10, right: 10 })}build() {Stack() {Scroll() {Column({ space: 10 }) {Image($r('app.media.mount')).width('100%').height(300)Tabs({ barPosition: BarPosition.Start }) {TabContent() {List({ space: 10 }) {ForEach(this.arr, (item: number) => {ListItem() {Text("item " + item).fontSize(20).fontColor(Color.Black)}.listCard()}, (item: number) => item.toString())}.edgeEffect(EdgeEffect.Spring).nestedScroll({scrollForward: NestedScrollMode.PARENT_FIRST,scrollBackward: NestedScrollMode.SELF_FIRST})}.tabBar("待处理")TabContent() {}.tabBar("已处理")}.scrollable(false) // 禁掉滚动.vertical(false).width("100%").height('calc(100% - 60vp)')}.width('100%')}.edgeEffect(EdgeEffect.Spring).friction(0.6).backgroundColor('#DCDCDC').scrollBar(BarState.Off).width('100%').height('100%').onDidScroll((xOffset: number, yOffset: number, scrollState: ScrollState): void => {// 累计计算当前父组件滚动在Y轴方向的偏移量this.curYOffset += yOffset// 根据父组件一共可以滚动的距离计算当前每帧的当前透明度let opacity = this.curYOffset / 240if (opacity >= 1) {opacity = 1}if (opacity <= 0) {opacity = 0}this.opacityNum = opacity})RelativeContainer() { //顶部菜单栏Text("返回").fontSize(16).fontColor(Color.Black).fontWeight(FontWeight.Medium).padding({ left: 20 }).height('100%').alignRules({top: { anchor: '__container__', align: VerticalAlign.Top },left: { anchor: '__container__', align: HorizontalAlign.Start }}).id('back')Text("标题").fontSize(16).fontColor(Color.Black).fontWeight(FontWeight.Medium).textAlign(TextAlign.Center).alignRules({top: { anchor: '__container__', align: VerticalAlign.Top },bottom: { anchor: '__container__', align: VerticalAlign.Bottom },left: { anchor: 'back', align: HorizontalAlign.End },right: { anchor: 'share', align: HorizontalAlign.Start }})Text("分享").fontSize(16).fontColor(Color.Black).fontWeight(FontWeight.Medium).padding({ right: 20 }).height('100%').alignRules({top: { anchor: '__container__', align: VerticalAlign.Top },right: { anchor: '__container__', align: HorizontalAlign.End }}).id('share')}.width('100%').height(44 + this.statusBarHeight).padding({ top: this.statusBarHeight }).position({ x: 0, y: 0 }).backgroundColor(`rgba(255,255,255,${this.opacityNum})`)}.height('100%').width('100%')}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/901477.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鲲鹏+昇腾部署集群管理软件GPUStack,两台服务器搭建双节点集群【实战详细踩坑篇】

前期说明 配置&#xff1a;2台鲲鹏32C2 2Atlas300I duo&#xff0c;之前看网上文档&#xff0c;目前GPUstack只支持910B芯片&#xff0c;想尝试一下能不能310P也部署试试&#xff0c;毕竟华为的集群软件要收费。 系统&#xff1a;openEuler22.03-LTS 驱动&#xff1a;24.1.rc…

React中 点击事件写法 的注意(this、箭头函数)

目录 ‌1、错误写法‌&#xff1a;onClick{this.acceptAlls()} ‌2、正确写法‌&#xff1a;onClick{this.acceptAlls}&#xff08;不带括号&#xff09; 总结 方案1&#xff1a;构造函数绑定 方案2&#xff1a;箭头函数包装方法&#xff08;更简洁&#xff09; 方案3&am…

【路由交换方向IE认证】BGP选路原则之Weight属性

文章目录 一、路由器BGP路由的处理过程控制平面和转发平面选路工具 二、BGP的选路顺序选路的前提选路顺序 三、Wight属性选路原则规则9与规则11的潜移默化使用Weight值进行选路直接更改Weight值进行选路配合使用route-map进行选路 四、BGP邻居建立配置 一、路由器BGP路由的处理…

Missashe考研日记-day20

Missashe考研日记-day20 1 高数 学习时间&#xff1a;2h30min学习内容&#xff1a; 今天当然是刷题啦&#xff0c;做不等式的证明板块的真题&#xff0c;证明题懂的都懂&#xff0c;难起来是真的一点思路都没有&#xff0c;这个板块还没做完&#xff0c;做完再总结题型。 2…

了解JVM

一.JVM概述 1.JVM的作用 把字节码编译为机器码去执行,负责把字节码装载到虚拟机中 现在的 JVM 不仅可以执行 java 字节码文件,还可以执行其他语言编译后的字节码文件,是一个跨语言平台 2.JVM的组成部分 类加载器&#xff08;ClassLoader&#xff09;运行时数据区&#x…

LeetCode LCR157 套餐内商品的排列顺序

生成字符串的全部排列&#xff08;去重&#xff09;&#xff1a;从问题到解决方案的完整解析 问题背景 在编程和算法设计中&#xff0c;生成字符串的所有排列是一个经典问题。它不仅出现在算法竞赛中&#xff0c;也在实际开发中有着广泛的应用&#xff0c;比如生成所有可能的…

pgsql:关联查询union(并集)、except(差集)、intersect(交集)

pgsql:关联查询union(并集)、except(差集)、intersect(交集)_pgsql except-CSDN博客

微信小程序中使用ECharts 并且动态设置数据

项目下载地址 GitHub 地址 https://github.com/ecomfe/echarts-for-weixin 将当前文件夹里的内容拷贝到项目中 目录&#xff1a; json: {"usingComponents": {"ec-canvas": "../components/ec-canvas/ec-canvas"} }wxml&#xff1a; <ec…

RV1126 人脸识别门禁系统解决方案

1. 方案简介 本方案为类人脸门禁机的产品级解决方案,已为用户构建一个带调度框架的UI应用工程;准备好我司的easyeai-api链接调用;准备好UI的开发环境。具备低模块耦合度的特点。其目的在于方便用户快速拓展自定义的业务功能模块,以及快速更换UI皮肤。 2. 快速上手 2.1 开…

深度学习ResNet模型提取影响特征

大家好&#xff0c;我是带我去滑雪&#xff01; 影像组学作为近年来医学影像分析领域的重要研究方向&#xff0c;致力于通过从医学图像中高通量提取大量定量特征&#xff0c;以辅助疾病诊断、分型、预后评估及治疗反应预测。这些影像特征涵盖了形状、纹理、灰度统计及波形变换等…

DeepSeek 接入 Word 完整教程

一、前期准备 1.1 注册并获取 API 密钥 访问 DeepSeek 平台&#xff1a; 打开浏览器&#xff0c;访问 DeepSeek 官方网站&#xff08;或您使用的相应平台&#xff09;。注册并登录您的账户。 创建 API 密钥&#xff1a; 在用户控制面板中&#xff0c;找到“API Keys”或“API…

驱动开发硬核特训 · Day 7:深入掌握 Linux 驱动资源管理机制(Resource Management)

&#x1f50d; B站相应的视屏教程&#xff1a; &#x1f4cc; 内核&#xff1a;博文视频 - 总线驱动模型实战全解析 —— 以 PCA9450 PMIC 为例 敬请关注&#xff0c;记得标为原始粉丝。 &#x1f6a9; 在 Linux 驱动开发中&#xff0c;资源管理机制决定了驱动的稳定性与可靠性…

什么是TensorFlow?

TensorFlow 是由 Google Brain 团队开发的开源机器学习框架&#xff0c;被广泛应用于深度学习和人工智能领域。它的基本概念包括&#xff1a; 1. 张量&#xff08;Tensor&#xff09;&#xff1a;在 TensorFlow 中&#xff0c;数据以张量的形式进行处理。张量是多维数组的泛化…

【ChCore Lab 01】Bomb Lab 拆炸弹实验(ARM汇编逆向工程)

文章目录 1. 前言2. 实验代码版本问题3. 关于使用问题4. 宏观分析5. read_line 函数介绍6. phase_0 函数6.1. read_int 函数6.2. 回到 phase_0 函数继续分析6.3. 验证结果 7. phase_1 函数7.2. 验证结果 8. phase_2 函数8.1. read_8_numbers 函数8.2. 回到 phase_2 函数继续分析…

《Vue Router实战教程》20.路由懒加载

欢迎观看《Vue Router 实战&#xff08;第4版&#xff09;》视频课程 路由懒加载 当打包构建应用时&#xff0c;JavaScript 包会变得非常大&#xff0c;影响页面加载。如果我们能把不同路由对应的组件分割成不同的代码块&#xff0c;然后当路由被访问的时候才加载对应组件&am…

docker 多主机容器组网

一、服务器A 1、初始化Swarm集群&#xff08;管理节点&#xff09; docker swarm init --advertise-addr 主节点ip 2、获取工作节点​​加入Swarm集群所需的Token 和完整命令 docker swarm join-token worker 3、创建Overlay网络 docker network create -d overlay --subnet…

rancher 解决拉取dashboard-shell镜像失败的问题

问题背景 在 Kubernetes 集群中部署 Rancher 后&#xff0c;点击右上角的 "Shell" 按钮时&#xff0c;Rancher 会动态创建一个 dashboard-shell-xxxxx Pod&#xff0c;用于提供 Web 终端功能。然而&#xff0c;由于默认镜像 rancher/shell:v0.1.21 托管在 Docker Hu…

OpenCV day2

Matplotlib相关知识 Matplotlib相关操作&#xff1a; import numpy as np from matplotlib import pyplot as pltx np.linspace(0, 2 * np.pi, 100) y1 np.sin(x) y2 np.cos(x)# 使用红色虚线&#xff0c;圆点标记&#xff0c;线宽1.5&#xff0c;标记大小为6绘制sin plt.p…

【网络安全】通过 JS 寻找接口实现权限突破

未经许可,不得转载。 本文所述所有风险点均已修复。 文章目录 引言正文引言 以下些漏洞已被起亚方面修复;起亚方面确认,这些漏洞从未被恶意利用过。 2024年6月11日,我们发现起亚汽车存在一系列严重安全漏洞,攻击者仅凭车牌号即可远程控制车辆的核心功能。该攻击不需要接触…

LabVIEW 发电机励磁系统监测与诊断

在现代工业体系中&#xff0c;发电机作为关键的电能转换设备&#xff0c;其稳定运行对于电力供应的可靠性起着决定性作用。而励磁系统作为发电机的核心控制部分&#xff0c;直接影响着发电机的性能和电力系统的稳定性。一旦励磁系统出现故障&#xff0c;可能引发发电机电压波动…