HarmonyOS 鸿蒙Next 实现渐变标题栏加吸顶分类栏实现

HarmonyOS 鸿蒙Next 实现渐变标题栏加吸顶分类栏实现 实现渐变标题栏加吸顶分类栏实现

2 回复

import { window } from ‘@kit.ArkUI’; @Component struct NestedScroll { @State currentYOffset: number = 0; private screenHeight?: number = AppStorage.get(‘screenHeight’) private statusBarHeight?: number = AppStorage.get(‘statusHeight’) private titleBarHeight: number = 40 + (this.statusBarHeight ? this.statusBarHeight : 0) @State tabHeight: string = (this.screenHeight ? (this.screenHeight - this.titleBarHeight) / this.screenHeight : 1) * 100 + ‘%’ private arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9]; private scrollerForScroll: Scroller = new Scroller(); private scrollerForList: Scroller = new Scroller(); controller: TextInputController = new TextInputController() win = window.getLastWindow(getContext()) change(): void { window.getLastWindow(getContext(), (err, data) => { let win: window.Window; if (err.code) { console.error(“error code :” + JSON.stringify(err.code)) return; } try { win = data; //设置窗口为全屏模式 win.setWindowLayoutFullScreen(true); // 设置状态栏 win.setWindowSystemBarProperties({ // 设置状态栏颜色为其他颜色 // statusBarColor: this.setStatusBarColor.toString(), // // 设置状态栏文本颜色为白色 // statusBarContentColor: ‘#ff81f362’ }) console.info(‘带状态栏沉浸式窗口设置完成’) } catch (expextion) { console.error(“error cause :” + JSON.stringify(expextion)) } }) } aboutToAppear(): void { this.change() } build() { Stack({ alignContent: Alignment.Top }) { Scroll(this.scrollerForScroll) { Column() { Column(){}.width(“100%”).height(200).backgroundColor(Color.Pink) Tabs({ barPosition: BarPosition.Start }) { TabContent() { List({ space: 10, scroller: this.scrollerForList }) { ForEach(this.arr, (item: number) => { ListItem() { Text(“促销商品” + item) .width(“100%”) .height(“100%”) .borderRadius(15) .fontSize(24) .textAlign(TextAlign.Center) .backgroundColor(Color.White) }.width(“100%”).height(100) }, (item: string) => item) } .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST }) .padding({ left: 10, right: 10 }) .width(“100%”) .edgeEffect(EdgeEffect.None) .scrollBar(BarState.Off) }.tabBar(‘促销活动’) TabContent() { List({ space: 10, scroller: this.scrollerForList }) { ForEach(this.arr, (item: number) => { ListItem() { Text(“行程安排” + item) .width(“100%”) .height(“100%”) .borderRadius(15) .fontSize(24) .textAlign(TextAlign.Center) .backgroundColor(Color.White) }.width(“100%”).height(100) }, (item: string) => item) } .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST }) .padding({ left: 10, right: 10 }) .width(“100%”) .edgeEffect(EdgeEffect.None) .scrollBar(BarState.Off) }.tabBar(‘行程服务’) } .vertical(false) .barMode(BarMode.Fixed) .barWidth(360) .barHeight(56) .width(“100%”) .height(this.tabHeight) .backgroundColor(’#F1F3F5’) } } .scrollBar(BarState.Off) .width(“100%”) .height(“100%”) .onWillScroll((xOffset: number, yOffset: number) => { this.currentYOffset = this.scrollerForScroll.currentOffset().yOffset; }) Row() { TextInput({ text: ‘’, placeholder: ‘input your word…’, controller: this.controller }).fontSize(24) } .justifyContent(FlexAlign.Center) .backgroundColor(Color.Gray) .width(‘100%’) .height(this.titleBarHeight) //渐变色范围150, 需要更改为(粉色图片高度 - titleBar高度 - 状态栏高度) .opacity(this.currentYOffset > 0 ? this.currentYOffset / 150 : 0) .padding({ top: this.statusBarHeight }) } .width(‘100%’) .height(‘100%’) .backgroundColor(0xDCDCDC) }


饭喂到嘴里

更多关于HarmonyOS 鸿蒙Next 实现渐变标题栏加吸顶分类栏实现的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)系统中实现渐变标题栏加吸顶分类栏,你可以通过以下步骤进行:

  1. 渐变标题栏

    • 使用ComponentAnimator组件来实现渐变效果。
    • 定义一个渐变动画,比如从透明到不透明的变化,或者颜色的渐变。
    • 在标题栏的onAppearonDisappear事件中触发动画,以实现进入或离开页面时的渐变效果。
  2. 吸顶分类栏

    • 使用ScrollViewList组件来承载内容,并设置其滚动监听。
    • 创建一个固定的分类栏,通常是一个ContainerStack组件,放置在页面顶部但不在ScrollViewList内部。
    • 通过监听ScrollViewList的滚动事件,动态调整分类栏的透明度或显示状态,实现吸顶效果。当滚动到一定位置时,使分类栏完全显示且不随内容滚动;未达到该位置时,可以隐藏或显示部分透明度。

实现过程中,你可能需要自定义一些布局和动画,以确保渐变和吸顶效果符合设计要求。同时,注意性能优化,特别是在处理大量数据或复杂动画时。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部