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.pswp.cn/news/901477.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/901477.shtml
英文地址,請注明出處:http://en.pswp.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;可能引發發電機電壓波動…