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

本文主要是介绍微信小程序 、taro3.x、uniapp 自定义导航栏头部、滑动渐变(仿小米Life小程序首页),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

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


前言

提示:一个小功能大致的逻辑:
1、自定义顶部导航栏:
(1)、随着需求不断的变化,小程序原生导航栏头部已不满足现在的社会的客户了
(2)、顶部导航栏我们可以获取手机系统的信息,完全可以适配其他手机的尺寸Taro.getSystemInfoSync()
(3)、看得出顶部导航栏由状态栏大小 + 小程序胶囊大小 + 胶囊距离状态栏高度 + 胶囊底部高度 = 总的高度,胶囊的信息获取 Taro.getMenuButtonBoundingClientRect()
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 = Taro.getSystemInfoSync()// 设置状态栏的高度this.state.statusBarHeight = info.statusBarHeightthis.state.windowWidth = info.windowWidth// h5 app mp-alipay// 获取胶囊的位置const menuButtonInfo = Taro.getMenuButtonBoundingClientRect()// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏的高度) = 导航栏的高度this.state.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - 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 / this.state.totalHeight;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);}}}
}

3.index.config.js

代码如下(示例):

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

总结

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

这篇关于微信小程序 、taro3.x、uniapp 自定义导航栏头部、滑动渐变(仿小米Life小程序首页)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/569314

相关文章

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T