微信小程序 、taro3.x、uniapp 自定义导航栏头部、滑动渐变(仿小米Life小程序首页)

阅读: 评论:0

微信小程序 、taro3.x、uniapp 自定义导航栏头部、滑动渐变(仿小米Life小程序首页)

微信小程序 、taro3.x、uniapp 自定义导航栏头部、滑动渐变(仿小米Life小程序首页)

文章目录

  • 前言
  • 一、效果(小米小程序)
  • 二 、我仿的效果
  • 三、代码逻辑
    • 1.index.jsx
    • 2.index.less
    • fig.js
  • 总结


前言

提示:一个小功能大致的逻辑:
1、自定义顶部导航栏:
(1)、随着需求不断的变化,小程序原生导航栏头部已不满足现在的社会的客户了
(2)、顶部导航栏我们可以获取手机系统的信息,完全可以适配其他手机的尺寸
(3)、看得出顶部导航栏由状态栏大小 + 小程序胶囊大小 + 胶囊距离状态栏高度 + 胶囊底部高度 = 总的高度,胶囊的信息获取
2、功能渐变效果:
(1)、顶部背景颜色根据滑动的位置了渐变,用背景颜色background: rgba()
(2)、搜索框适配手机,默认原来的宽度 + 滑动距离的剩余的跨度
(3)、logo图标随着话滑动距离的大小渐变


提示:以下是本篇文章正文内容,下面案例可供参考

一、效果(小米小程序)

二 、我仿的效果

三、代码逻辑

1.index.jsx

代码如下(示例):

import { Component } from 'react'
import { View, Image, Text } from '@tarojs/components'
import Taro from '@tarojs/taro'
// import Taro, { Component } from "@tarojs/taro"
// import { AtButton } from 'taro-ui'
import topIcon from '../../assets/images/pay_type0.png'
import './index.less'export default class Index extends Component {constructor(props) {super(props);this.state = {totalHeight: "",	// 总高度statusBarHeight: "",	// 状态栏高度navBarHeight: 45,	// 导航栏高度windowWidth: 375,navStyle: "",navOpacity: 0,navInpWid: "",navRemain: "",widRemain: "",scrollTop: 0,imgOpacity: 1,};}componentWillMount () { const info = SystemInfoSync()// 设置状态栏的高度this.state.statusBarHeight = info.statusBarHeightthis.state.windowWidth = info.windowWidth// h5 app mp-alipay// 获取胶囊的位置const menuButtonInfo = MenuButtonBoundingClientRect()// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏的高度) = 导航栏的高度this.state.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (p - info.statusBarHeight) + 4this.state.windowWidth = menuButtonInfo.leftthis.state.widRemain = (info.windowWidth / 375 * 70)this.setState({navInpWid: this.state.windowWidth - this.state.widRemain,navRemain: this.state.windowWidth - this.state.widRemain,totalHeight: this.state.statusBarHeight + this.state.navBarHeight})}onPageScroll(e) {let topHeight = e.scrollTop, navOp = 0navOp = topHeight / alHeight;let mobileTop = this.state.navRemainthis.setState({navOpacity: navOp,navInpWid: navOp > 0 ?  mobileTop + (this.state.widRemain * navOp) : this.state.navRemain,imgOpacity: navOp <= 1 ? 1 - navOp : 0})const styles = `background: rgba(255, 255, 255, ${navOp});`this.setState({navStyle: topHeight > 10 ?  styles : ""})}render () {const {totalHeight, navBarHeight, statusBarHeight, windowWidth, navStyle, navOpacity, navInpWid, imgOpacity} = this.statereturn (<View className='index'><View className='top_navbar'><View className='top_pos' style={`height:${totalHeight}px`}></View><View className='navbar-fixed' style={navStyle}><View style={`height:${statusBarHeight}px`}></View>{/* <View style={{height: statusBarHeight+'px'}}></View> <View className='navbar-content' style={{height: navBarHeight+'px', width:windowWidth+'px'}}> */}<View className='navbar-content' style={`height:${navBarHeight}px; width:${windowWidth}px`}><Image style={`opacity:${imgOpacity}`} className='top_icon' src='../../assets/images/pay_type0.png'></Image><View style={`width:${navInpWid}px`} className={navOpacity >= 1 ? 'nav_input nav_inp_ac' :'nav_input'}></View></View></View></View><View style='width: 100%; height: 500px; background: #FE5804'></View><View style='width: 100%; height: 500px; background: pink'></View><View style='width: 100%; height: 500px; background: green'></View><View style='width: 100%; height: 500px; background: red'></View>{/* <AtButton type='primary'>按钮文案</AtButton> */}</View>)}
}

2.index.less

代码如下(示例):

.top_navbar{.top_pos{width: 100%;position: fixed;top: 0;left: 0;background-color: #FE5804;}.navbar-fixed{position: fixed;top: 0;left: 0;z-index: 100;width: 100%;.navbar-content {display: flex;align-items: center;justify-content: flex-end;height: 45px;padding: 0 20px 0 30px;box-sizing: border-box;position: relative;.top_icon{position: absolute;left: 30px;width: 50px;height: 50px;display: block;z-index: -1;border-radius: 50%;}.nav_input{width: 430px;height: 70px;border-radius: 40px;margin-bottom: 8px;background-color: #fff;}.nav_inp_ac{transition: all ease 0.3s;background-color: #F7F7F7;}.return_icon {width: 54rpx;height: 54rpx;margin-bottom: 4px;display: block;border-radius: 50%;}.return_icon2 {width: 22rpx;height: 38rpx;transform: rotateY(180deg);}}}
}

fig.js

代码如下(示例):

export default {navigationBarTitleText: '',navigationStyle: 'custom',
}

总结

以上就是仿小米的效果内容,本文仅仅简单介绍了**SystemInfoSync()** 的使用来适配,使用**MenuButtonBoundingClientRect()** 等等api,逻辑并不是难

本文发布于:2024-01-31 04:43:24,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170664740725580.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:程序   自定义   小米   首页   头部
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23