小程序setNavigationBarColor设置背景渐变,图片渐变

小程序setNavigationBarColor设置背景渐变

第一种:渐变图片

html 复制代码
<view class="Box" :style="'height:'+s_topImg+'rpx'" >
			<!-- 设置背景图片及自适应高度 -->
			<view class="contain-box" :style="[{backgroundImage:'url('+backgroundImg+')'}, 'height:'+s_topImg+'rpx']" >
				<!-- 滚动动态设置样式 -->
				<view :class="scrollTopShow?'top-item1':'top-item2'":style="'background:rgba(48, 154, 236,' +topOpacity+')'">
					<!-- 胶囊以上占位盒子 -->
					<view :style="'height:'+s_top+'rpx', 'line-height:'+s_top+'rpx'"></view>
					<!-- 动态设置高和行高与胶囊保持一致 -->
					<view class="title-type" :style="['height:'+s_height+'rpx','line-height:'+s_height+'rpx']">
						项目订阅管理
					</view>
				</view>
			</view>
		</view>

<script>
	export default {
		data() {
			return {
				backgroundImg: '/subscribe/static/img/topbg1.jpg', // 顶部背景图
				s_top: '100', //胶囊距离顶部距离
				s_height: '100', //胶囊行高	
				s_topImg: '80',
				scrollTopShow: true, // 顶部颜色默认隐藏
				topOpacity: 0,
			}
		},
		created() {
			this.initTopImg();
		},
		// 监听滚动事件
		onPageScroll(e) { //nvue暂不支持滚动监听,可用bindingx代替
			let scrollTop = e.scrollTop;
			this.topOpacity = scrollTop / 300 > 0.9 ? 1 : scrollTop / 300
			if (e.scrollTop != 0) {
				this.scrollTopShow = false;
			} else {
				this.scrollTopShow = true;
			}
		},
		methods: {
			// 初始化顶部背景图
			initTopImg() {
				let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
				this.s_top = menuButtonInfo.top * 2;
				this.s_topImg = menuButtonInfo.top * 2+100 ;//+ 508
				this.s_height = menuButtonInfo.height * 2;
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f9fa;
	}
	.Box{
		position: sticky;
		    top: 0;
		    z-index: 100;
	}
	.contain-box {
			width: 100%;
			background-size: 100% 100%;
			align-items: center;
	
			.top-item1 {
				.title-type {
					font-size: 36rpx;
					font-weight: 400;
					color: #fff;
					display: flex;
					justify-content: center;
					/* 水平居中 */
				}
			}
	
			.top-item2 {
				position: fixed;
				top: 0;
				width: 100%;
	
				.title-type {
					font-size: 36rpx;
					font-weight: 400;
					color: #fff;
					display: flex;
					justify-content: center;
					/* 水平居中 */
				}
			}
		}
	.top{
	   width: 100%;
	   height: 200px;
	   background: linear-gradient(121deg, #94C7FE -1%, #1890FF 102%);
	   box-shadow: 24px 52px 100px 0px rgba(90, 108, 234, 10%)
	}

	.setting{
		.header {
			background: linear-gradient(135deg, #007aff 0%, #0056d6 100%);
			color: white;
			padding: 20px 16px;
			text-align: center;
			// position: sticky;
			// top: 0;
			z-index: 100;
			box-shadow: 0 2px 10px rgba(0, 122, 255, 0.2);
		}
		
		.header h1 {
			font-size: 1.25rem;
			font-weight: 600;
			margin-bottom: 4px;
		}
		
		.header p {
			font-size: 0.875rem;
			opacity: 0.9;
		}
		
		.main {
			padding: 16px;
			padding-bottom: 100px;
		}
		
		.list {
			background: white;
			border-radius: 12px;
			margin-bottom: 16px;
			padding: 16px;
			box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
			transition: all 0.3s ease;
		}
		
		.list:active {
			transform: scale(0.99);
		}
		
		.first {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 12px;
		}
		
		.title {
			font-size: 1.125rem;
			font-weight: 600;
			color: #2c3e50;
		}
		
		.subtitle {
			font-size: 0.75rem;
			padding: 4px 10px;
			border-radius: 10px;
			font-weight: 500;
		}
		
		/* 分类标识颜色 - 使用主题色变体 */
		.color1 {
			background-color: rgba(0, 122, 255, 0.1);
			color: #007aff;
		}
		
		.color2 {
			background-color: rgba(0, 122, 255, 0.15);
			color: #0062cc;
		}
		
		.color3 {
			background-color: rgba(0, 122, 255, 0.2);
			color: #0052b3;
		}
		
		.content {
			margin: 12px 0;
			padding: 12px;
			background: #f9f9f9;
			border-radius: 8px;
		}
		
		.second {
			display: flex;
			flex-direction: column;
		}
		
		.secondTip {
			font-weight: 600;
			color: #2c3e50;
			margin-bottom: 8px;
			font-size: 0.875rem;
		}
		
		.secondList {
			display: flex;
			flex-wrap: wrap;
			gap: 8px;
		}
	
	}

	
</style>

2. 渐变颜色

<view class="Box" :style="'height:'+s_topImg+'rpx'" @touchmove.prevent>

<uni-icons type="left" size="25" @click="goBack" class="iconback">
项目订阅管理

相关推荐
djxzyr1 天前
微信小程序传感器实战:罗盘、加速计、陀螺仪与加速度实时波形
微信小程序·小程序
李纲明3 天前
微信小程序开了 Skyline 后样式全乱、滚动对不上甚至部分组件不能用?Skyline 怎么接才稳
javascript·微信小程序·小程序·devdiy
wx1qq1com5 天前
一桩带几台车:共享租赁桩位配比、取电与结算闭环设计
物联网·微信小程序·小程序
2401_879692515 天前
微信小程序照相与摄像功能实现——chooseImage 与 chooseVideo 详解
微信小程序·小程序·notepad++
李纲明7 天前
微信小程序 getSystemInfo 提示已废弃、屏幕高度算错胶囊还对不齐?getWindowInfo 和 getDeviceInfo 怎么接才稳
javascript·微信小程序·小程序·小程序开发·devdiy
dreams_dream7 天前
微信小程序-使用npm包
微信小程序·小程序·npm
caoliaoerweima7 天前
草料二维码小程序上线活码编辑器:手机上也能编辑活码了
智能手机·小程序·编辑器·草料二维码
dreams_dream10 天前
微信小程序:页面事件-上拉触底
微信小程序·小程序
wafrdggh10 天前
2026电商快递批量查询怎么用?固乔助手模板设置评测
大数据·数据库·小程序
wafrdggh11 天前
2026 商城小程序平台哪个价格合理?把跨境附加成本这个坑算透
大数据·小程序