排行榜 统计
  • 文章总数:1526 篇
  • 评论总数:5 条
  • 分类总数:7 个
  • 最后更新:21秒前

微信小程序自定义顶部导航,滚动页面顶部导航颜色渐变

本文阅读 2 分钟
首页 资讯 正文

微信小程序自定义顶部导航栏,使背景图置顶;当向上滚动页面时,实现顶部导航颜色渐变


效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实现方法

一、在pages.json中设置"navigationStyle": “custom”

代码如下(示例):

"path": "pages/home/home",
"style": {
	"navigationBarTitleText": "XX网络科技",
	"navigationBarBackgroundColor": "#309AEC",
	"navigationBarTextStyle": "white",
	"enablePullDownRefresh": false,
	"backgroundColor": "#ffffff",
	"navigationStyle": "custom"
	}						
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

二、在页面中配置

提示:由于不同的手机机型顶部导航高度不一样,所有要获取手机的信息

实现方法

总共三步:
1、初始化获取顶部导航信息
2、顶部导航文字上方通过view占位,同时设置顶部导航字体高和行高与胶囊保持一致,实现顶部导航文字在不同机型居中对齐
3、监听滚动事件,不滚动时不加颜色,滚动时给顶部加动态样式

代码如下(示例):

<template>
	<view class="Box">
		<!-- 设置背景图片及自适应高度 -->
		<view class="contain-box" :style="[{backgroundImage:'url('+backgroundImg+')'}, 'height:'+s_topImg+'rpx']" v-for="(item,index) in swipers" :key="index">
			<!-- 滚动动态设置样式 -->
			<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']">
					XX网络科技
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				backgroundImg: '', 
				s_top: '', 
				s_height: '', 
				s_topImg: '',
				scrollTopShow: true, 
				topOpacity: 0,
			}
		}created() {
			this.initTopImg();
		},
		
		onPageScroll(e) { 
				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 + 508;
				this.s_height = menuButtonInfo.height * 2;
			},
			
		}
	}
</script>
<style lang="scss" scoped>
	.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;
				
			}
		}
	}
</style>
本文来自投稿,不代表本站立场,如若转载,请注明出处:https://typecho.firshare.cn/archives/2140.html
免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。避免网络欺诈,本站不倡导任何交易行为。如您私自与本站转载自公开互联网中的资讯内容中提及到的个人或平台产生交易,则需自行承担后果。本站在注明来源的前提下推荐原文至此,仅作为优良公众、公开信息分享阅读,不进行商业发布、发表及从事营利性活动。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。
-- 展开阅读全文 --
WordPress主题开发(六)之—— 模版文件和自定义模板进阶
« 上一篇 03-15
VirboxLM 许可借阅
下一篇 » 03-15