【Uniapp小程序】自定义导航栏uni-nav-bar滚动渐变色

效果图

新建activityScrollTop.js作为mixins

javascript 复制代码
export default {
  data() {
    return {
      navBgColor: "rgba(0,0,0,0)", // 初始背景颜色为完全透明
      navTextColor: "rgba(0,0,0,1)", // 初始文字颜色
    };
  },
  onPageScroll(e) {
    // 设置背景
    const newAlpha = Math.min((e.scrollTop / 100) * 1, 1);
    this.navBgColor = `rgba(${this.shadeBackground},${newAlpha})`;
    // 设置文字
    const progress = Math.min(e.scrollTop, 255) / 255;
    const [r, g, b] = this.shadeTextColor.split(",").map(Number);
    const newTextRgb = Math.min(e.scrollTop * 5, 255);
    const newRgb = [r, g, b].map((targetValue) => {
      return Math.min(newTextRgb + (targetValue - newTextRgb) * progress, 255);
    });
    this.navTextColor = `rgba(${newRgb.join()},1)`;
  },
};

使用方法

第一步,引入上方js

javascript 复制代码
import activityScrollTop from "../../js/activityScrollTop";
export default {
  mixins: [activityScrollTop],
}

第二步:

关键:backgroundColor和color

javascript 复制代码
<uni-nav-bar
  :leftIcon="图标"
  :border="false"
  @clickLeft="方法名"
  :backgroundColor="navBgColor"
  fixed
  statusBar
  :color="navTextColor"
  title="活动详情"
></uni-nav-bar>

第三步:

data

javascript 复制代码
// 渐变导航背景颜色
shadeBackground: "254,181,89",
// 渐变导航的文字颜色
shadeTextColor: "255,255,255",

看效果即可!

感谢你的阅读,如对你有帮助请收藏+关注!

只分享干货实战 和精品 ,从不啰嗦!!!

如某处不对请留言评论,欢迎指正~

博主可收徒、常玩QQ飞车,可一起来玩玩鸭~

相关推荐
MoonMoonLee3 天前
小程序配置
小程序
码兄科技4 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
2501_916007474 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
JQweryuiop4 天前
中小型游戏陪练工作室数字化管理实践:基于七彩屋电竞护航小程序订单、履约与绩效系统的落地复盘
大数据·游戏·小程序·架构
Ai-_Man4 天前
您您这可以把Mistral AI的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。
人工智能·ai·小程序·电脑
2501_916008894 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
微擎应用市场4 天前
简单拼车小程序系统:实现出行与物流资源的高效精准匹配
小程序
2501_916008894 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
2601_963870204 天前
基于springboot的综合网上购物商场系统
微信小程序·小程序·课程设计
2601_963869954 天前
力序健身房管理系统
微信小程序·小程序